• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difference between if(u>0) and if((u&1)>0)

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic