0

So I saw this code which printed out individual bits of any number.I do not understand why the individual bits are accessed and not the entire number itself

#include <stdio.h>
int main()
{
    int x=10, b;   
    for(b=0; x!=0; x>>=1) {
        printf("%d:%d\n", b, (x&1));
        b++;   
    }
}

OUTPUT:

0:0 
1:1 
2:0 
3:1 

Please help me understand this piece of code.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Deba
  • 131
  • 5
  • There is no way to access individual bites. It just obtains a sequence of `0` and `1` integers using common practices. – StoryTeller - Unslander Monica Jan 02 '18 at 08:27
  • You should step through this code in debugger and watch variable values as you do so. Then it will be obvious what is happening. – user694733 Jan 02 '18 at 08:28
  • 3
    Possible duplicate of [How do you set, clear, and toggle a single bit?](https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit) – Micha Wiedenmann Jan 02 '18 at 08:28
  • 1
    Do you understand what `x&1` is? Can you describe that expression with the help of the term "LSB" ("least signifying bit"). Do you understand what happens to `x` during `x>>=1` ? Can you describe that statement with the help of the term "right-shift"? If you can't, please read up on those operators. If you can, what is your problem otherwise? – Yunnosch Jan 02 '18 at 08:33

2 Answers2

0

There are two questions in your post: how to access an individual bit ? and how to select that bit ?

Concerning the first question, suppose you want to access the less significant bit (or, to make it simpler, the rightmmost bit), you can use a mask: suppose your data is b0011 for instance, you can mask with b0001 (i.e. 1 in decimal).

  0 0 1 1
& 0 0 0 1
---------
  0 0 0 1

The & operator is the bitwise and. If you look in your code sample, you have printf("%d:%d\n", b, (x&1)); in which you can see x & 1, i.e. print the rightmost bit of x.

Now comes the second question: how to put each bit in the rightmost position one after each other (said otherwise, how to select the bit to print) ? An easy solution is to shift your data of 1 position to the right each time you want to select the next bit (i.e. the bit to the left of the current one).

In C, you can shift using >>. For instance, if x is b0011, then x >> 1 is b0001 (in this case, you fill the leftmost position with zeros, but in some cases it might be trickier). If you look in you code sample, you have x>>=1 in the for-loop, which assigns x >> 1 in x.

Hence, suppose you take the previous example:

  0 0 1 1 = x                       0 0 0 1 = x
& 0 0 0 1                         & 0 0 0 1
---------    x >> 1 = b0001 -> x  ---------
  0 0 0 1                           0 0 0 1

and so one...

A last bonus point, the loop stopping condition is x != 0, this implies that you don't prints all bits of your data, but only the bits up to the leftmost 1 (included). For instance, in the above example, after printing the two rightmost bits, x becomes 0 and the loop exits.

Bromind
  • 1,065
  • 8
  • 23
0

In your code you are printing the value of X variable in binary. For this, your code, use logical operation as AND operator and right-shift.

In the loop condition, you displace the X variable one bit to the right.

 for b = 0 you get x = 1010  
 for b = 1 you get x = 101  
 for b = 2 you get x = 10  
 for b = 3 you get x = 1

Then, in your print, show your loop iterator (b) and your X variable AND 1.

The AND operator get this values:

0 AND 0 = 0   
0 AND 1 = 0  
1 AND 0 = 0  
1 AND 1 = 1

In your case, you have:

1010 AND (000)1 = 0  
101 AND (00)1 = 1  
10 AND (0)1 = 0  
1 AND 1 = 1
Zharios
  • 183
  • 1
  • 18