Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if(x & 1) != 0


how does the statement work?
i can't get it
any help please??
Posted

It checks up if the first (least-significant) bit of x is set (equals to 1, not to 0).
Only the syntax requires brackets around to condition: if ((x & 1) != 0) ….

—SA
 
Share this answer
 
Comments
yamen-nassif 29-Mar-12 0:55am    
thx
Sergey Alexandrovich Kryukov 29-Mar-12 10:47am    
You are welcome.
--SA
ProEnggSoft 29-Mar-12 1:27am    
+5
Sergey Alexandrovich Kryukov 29-Mar-12 10:47am    
Thank you.
--SA
Monjurul Habib 30-Mar-12 4:52am    
5!
This is a bitwise operation. See here for more information:
http://www.leepoint.net/notes-java/data/expressions/bitops.html[^]
So, x&1 checks if the first bit is set, x&2 checks if the second bit is set and so on...
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 29-Mar-12 0:52am    
Sure a 5. (I even answered when the condition is true, please see... :-))
--SA
yamen-nassif 29-Mar-12 0:53am    
Ok with (x&1) i get it

what about (x&2) (x&4)

does it mean that i have to look at x in binary????
Sergey Alexandrovich Kryukov 30-Mar-12 11:20am    
Sure. 2 = 1 << 1 and 4 = 1 << 2, first and second bits. What's the problem?
--SA
Monjurul Habib 30-Mar-12 4:53am    
5!
Sergey Alexandrovich Kryukov 30-Mar-12 11:19am    
Thank you, Monjurul.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900