user1692517
user1692517

Reputation: 1152

could someone explain this small snippet of bit Operation

I'm a C noob, but have experience in Java and Python. I'm currently doing an assignment on bit operations and found a guide that showed me how to do it, problem is that I don't quite understand.

c=(c&(1<<n))>>n;

c = an unsigned char

n = integer, represents the nth bit of a c.

I understand that & = the AND logic gate, and I'm aware of how that works also. I understand that << = left shift, and >> = right shift.

However, I'm unable to comprehend how this all works together. Could someone explain how this code executes and how it's able to return the nth bit.

Upvotes: 0

Views: 93

Answers (3)

Sitses
Sitses

Reputation: 318

Also took me a second to figure it out (I'm used to do this another way), but here it is:

Let's start with the first thing the snippet does (because of the brackets).

1<<n

1 is written in binary like this 00000001 and to shift it to the left n steps, basically just shifts the 1, e.g. 2 steps would result in 00000100 and 5 in 00100000. So what you got is a number whith only the nth bit 'switched on'.

Then this number is AND with you char, so wich will either return the same number or 0. To see why this happens lets look at some example (read up to down):

Your number is   0 1 0 1 0 1 1 0
                 & & & & & & & &
And 1<<4 is      0 0 0 1 0 0 0 0
                 | | | | | | | |
result:          0 0 0 1 0 0 0 0

or

Your number is   0 1 0 1 0 1 1 0
                 & & & & & & & &
And 1<<5 is      0 0 1 0 0 0 0 0
                 | | | | | | | |
result:          0 0 0 0 0 0 0 0

So if the nth bit of c is 'switched on', the result after the AND logic gate also gonna have this bit set to 1.

Last but not least we want this bit not to be somewhere in our c but at the beginning (or to be honest the end, but you know what I mean, so that we can check if it is 0 or 1, because 00000001 equals 1 and 00000000 equals 0), so we shift the bit, to the last place (which is easy, because we know it is at the nth place, so we can just say, "Hey bit, move n-places to the left", e.g. 00010000>>4 equals 00000001).

And that's it, by the way, you could also use ((c<<n)>>n), that's what I usually use, and I think it's kind of easier to understand (or?)

Upvotes: 1

Moha the almighty camel
Moha the almighty camel

Reputation: 4443

c&(1<<n) this is testing to see if the nth bit is a one or zero.

after this operation you will have either all zeros, or n-1 zero followed by a one.

then applying >>n get the nth bit from the previous operation to be the LSB. and you assign that to the variable.

Upvotes: 0

aaronman
aaronman

Reputation: 18750

This code basically checks if the nth bit is set.

  1. 1 << n, puts 1 in the nth place
  2. c & (1 << n), if c has it's nth bit set the nth bit will be preserved, else the whole thing will be 0
  3. lastly shift it n back the other way to get the 1 in the first position, so 1 if the nth bit is set and 0 if it's not

you could also do.
!!(c&(1<<n))
same effect

Upvotes: 3

Related Questions