bitwise AND operator's use in Java

If I have the below code, I get a 3 for the final int variable.  Can someone explain how this operator really works...i thought it was comparing two numbers and giving a 1 or 0 depending if they matched or not...

int first= 7;
int second = 3;
int final = frist & second;
cgray1223Asked:
Who is Participating?
 
VenabiliCommented:
>i thought it was comparing two numbers and giving a 1 or 0 depending if they matched or not...

this is true but not for numbers - it is on the bit level. :)
3 is represented as 0011
7 is represented as 0111
Starting bit by bit - if both are 1, the result is 1, otherwise it is 0.
So the above leades to 0011 - which is 3.

Does it make sense?
0
 
VenabiliCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.