| Author |
difference between if(u>0) and if((u&1)>0)
|
Alfred Kemety
Ranch Hand
Joined: Aug 14, 2002
Posts: 279
|
|
if us is a long value that is always positive (unsigned long in C realm) What is the difference between if(u>0) and if((u&1)>0) I mean (u&1) will only return 0 if "u" actually equals 0 if it is equal to 1 or 2 (10) or 3 (11) or 4 (100) then it will not return 0. are am I mistaken? so what's the difference? There's a code snippet in a book that uses C to express algorithms and it uses if(u&1) which is equivilant to - I guess - if((u&1)>0) in java... what am I missing?
|
Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Hi Alfred, welcome back! u & 1 is telling you the state of the low-order bit of the (unsigned or signed) longword. u & 1 will be 0 or 1 depending on whether the low-order bit of u is 0 or 1 respectively. In C: if( x ){ // here if x != 0 } else { // here if x == 0 } Is that what you are asking? -Barry
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Alfred Kemety
Ranch Hand
Joined: Aug 14, 2002
Posts: 279
|
|
Ah, so it is some kind of check whether u is odd or even, true if odd and false if even... Thanks a lot.. I just got confused with the code for a second.. I guess I should take a break, been reading for the last 5 hours.. simple stuff seems really puzzely when one is tired...
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Alfred Kemety: Ah, so it is some kind of check whether u is odd or even, true if odd and false if even...
Yes, in a way, but odd and even are too, what's the word, specific. It could have been u & 2, and in that case it is checking the state of 2nd low order bit. If that u was some kind of device status register then the bit could represent whether or not interrupts were enabled.
|
 |
 |
|
|
subject: difference between if(u>0) and if((u&1)>0)
|
|
|