| bio | website | |
|---|---|---|
| location | The Msunduzi, South Africa | |
| age | ||
| visits | member for | 2 years, 1 month |
| seen | Apr 25 '12 at 12:46 | |
| stats | profile views | 4 |
I'm an IT teacher-in-training, doing a postgraduate certificate in education. I worked in industry for 2 years, developing enterprise applications in C# and then in maintenance of a VB6 system. I enjoy competitive algorithmic programming.
|
Dec 18 |
awarded | Notable Question |
|
Mar 29 |
awarded | Popular Question |
|
Apr 15 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? @Javier Ahhh thank you |
|
Apr 14 |
awarded | Editor |
|
Apr 14 |
revised |
How does optimization make code “greener”? fixed grammar in title |
|
Apr 14 |
suggested | suggested edit on How does optimization make code “greener”? |
|
Apr 14 |
awarded | Commentator |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? @Javier yay it compiles, but the "wrongness" is no longer illustrated if (((condA!=0) && (condB!=0))) {
System.out.println("correct");
}
if (((condA!=0) & (condB!=0))) {
System.out.println("never executed?");
} executes both print statements. |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? Nope @Javier: Cannot cast from int to boolean. |
|
Apr 14 |
awarded | Supporter |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? @Steven A. Lowe: if i=3 and j=2 then i&j = 2, while i&&j is an error This is brilliant! It’s simple and it makes the point. At 10th grade level this is also a likely mistake to make, since they are still getting used to the Boolean type and what the operators would apply to. Thank you so much! Great advice on not arguing with my mentor either. |
|
Apr 14 |
awarded | Scholar |
|
Apr 14 |
accepted | When is it appropriate to use a bitwise operator in a conditional expression? |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? Tfa. How do I explain to my 15 yr olds that 1 & is “harder to read” than 2? I don’t have an example in which the 2 operators don’t work in the same way for Boolean operands. I agree on your point about more readable and easier to maintain code. I want to encourage them to write beautiful code. But having some proof in my bag of tools would be more convincing than “because I said so”. I do have it stated as a standard at that link, and may have to rely on that alone if I don’t get the example I’m looking for. As I asked @Steve Haigh: should java be indicating this as an improper use? |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? Thanks to you I’ve learned an application of bitwise operators I hadn’t know about before! But in this example, only the bitwise operator applies. I’m looking for a piece of well-written code where using the bitwise rather than the conditional would lead to compilation, but an incorrect output. That is, if such a code snippet exists. |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? @Javier: Thank you for answering, but I’m a little confused. I’m working in java, and this code does not compile. (condA && condB) errors, because the && does not work for 2 ints, only 2 booleans. (condA & condB) while correct, evaluates to an int and in java we cannot say if(int) so it errors too. You’re the first to understand what I’m looking for though- exactly that example of wrongness. |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? Thank you for answering. Your paragraph 1 was what I was trying to get at in my paragraph 4, by stating that bitwise is an eager operator while the conditional behaves as a short circuit. Your paragraph 2 is the concern I described in my paragraph 5. So I’m aware of the difference but I’m looking for that specific example neither you nor I can think of. |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? @Steven A. Lowe: You are right and this is why I am against using the bitwise operators. My apologies if I had not made that clear. However I need to produce some evidence to this teacher-mentor as to why it is so undesirable. I am looking for an example of one of these unexpected behaviours you mention. The only example I found was the one above, and I think it isn’t suitable because of the reasons stated in paragraph 5. |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? Yes @Steve Haigh, the compiler doesn’t reject it, but it isn’t the right way to use them, judging from the published coding standard I linked to. Can I comfortably rest on dismissing it because it does not comply with a coding standard, or should java maybe indicate that this is an improper use? |
|
Apr 14 |
comment |
When is it appropriate to use a bitwise operator in a conditional expression? Thanks for answering @Steven A. Lowe. You said "|| and | and && and & are not interchangeable" but bitwise = !(true & true == false); and condition = !(true && true == false); will both evaluate to true, so in this case they are interchangeable? Syntactically perhaps, since the code still compiles. I would agree that they are used for different things semantically, as I mentioned in paragraph 2. You say that ! and & "almost never appear in a conditional by themselves". I am looking for these "almost never" cases, and wondering whether they do legitimately exist. |