Need help understanding this line

JSolano

thank you in advance for this basic question.

I am going through a tutorial and I see this line.

int a = (n & 8) / 8

This is supposed to identify whether the fourth bit from the right is a binary representation of 0 or 1. I understand the concept of bits etc, but I do not understand what mathematical equation (if any) this represents.

Would anyone care to explain how this would be written in a mathematical equation? Also, please let me know if i am missing anything else in my understanding of this line. Thank you.

Peter vdL
  1. The expression ( n & 8 ) does Logical And of n with 1000 binary.

  2. So that gets the 4th bit from right.

  3. then dividing that by 8, shifts the value right 3 binary places. I.e. it moves the 4th bit to the rightmost place.

That is more clearly expressed as " >> 3"

So your overall expression would be something like:

  (n AND 1000 )  >> 3

And that leaves the 4th bit of N in a temporary variable, as bit 0 (rightmost bit). All the other bits will be zero because of the AND.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related