-1

I'm struggling with the piece of code below, it's used to convert an integer into a binary. Can someone explain it more cearly? especially the '0'+

for (;d>0;d--){
    buffer[index++] = '0'+ (x & 1); 
    x >>= 1;
}
inControl
  • 2,215
  • 4
  • 24
  • 39

4 Answers4

1

This is a loop that starts with a variable containing some value and then creates a string of character digits of ones and zeros.

The '0' + (x & 1) takes the character for a digit zero '0' and then adds to that character the value of the right most bit of x which have either a value of zero or of one. If the bit is zero then the result of the addition is '0' and if the bit is one then the result of the addition is '1'.

This character is then put into the buffer, the variable x is right shifted by one bit to move the next binary digit to the right most place.

The addition is then repeated.

The result is that you have a text string of zeros and ones as character digits.

Are you sure this is the correct source code? Looks to me like the text string result would need to be reversed in order to correctly represent the binary value.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • But why start with a '0' doesn't (x & 1) return a 1 or a zero anyway? Or is this just to make it a string? – inControl Jun 11 '14 at 18:28
  • @inControl, the idea of the assignment is to convert a binary value of zero or one to a character digit of '0' or '1' by taking advantage of the ANSI character sequence (actually the same in UNICODE as well since the lower 256 are ANSI character set) where the character digits of '0' through '9' are in ascending order from 0x30 to 0x39. As each character digit is created, it is put into a an array element and then the array index is incremented to go to the next array element of the text string. – Richard Chambers Jun 11 '14 at 19:25
1
buffer[index++] = '0'+ (x & 1); 

This line progresses through what is presumably a char array, setting each character to the character '0' PLUS a value that will be equal to either 0 or 1. '0' + 0 is ''0'. '0' + 1 is '1'. The reason x & 1 will be either 0 or 1 is because this code is essentially checking if the low bit is on in x. The reason this works is because the line below then right shifts x by 1, then sets x equal to that value, which basically is knocking off the low bit, and shifting all other bits over by 1. In this way, x is traversed, and each bit is checked.

Please note, however. It appears that it will be written BACKWARDS!

Kik
  • 428
  • 3
  • 9
1

In ASCII 0 has the ASCII value 48 and 1 has the ASCII value 49. IOW if you write putchar(48); you see a 0 on the screen

The buffer presumably being a char 2-dimensional array is assigned either 48 or 49 because x & 1 evaluates to either 1 or 0.

so say you have a value x = 225 and want to convert it to readable text containing 0's and 1's

225 looks like this in binary

1110 0001

when you do 1110 0001 & 0x1 you mask out the last 1 left is 0000 0001

so adding 1 to 48 and converting the sum ito a character is 1

next the bits are shifted one step right x >>= 1

0111 0000

masking that with 0x1 is 0000 0000

so adding 0 to 48 and converting the sum to a character becomes 0

and so on until x is 0

AndersK
  • 35,813
  • 6
  • 60
  • 86
1

First of all, "index" is initialized to 0. But what is the definition of "d"? We have an array of characters named "buffer" and "x" is an integer to be converted. Now, in "x & 1", "&" is Bitwise AND operator. If we operate "x & n", it changes the last n least significant bits as,

1 & 1 = 1,
1 & 0 = 0,
0 & 1 = 0,
0 & 0 = 0.

if we execute 4 & 1,

100
001
---
000

then, it returns 0.

if we execute 9 & 1,

1001
0001
----
0001

then, it returns 1.

Basically, if x is a even number x&1 returns 0, or returns 1 if x is odd. Now, after that, this 0 or 1 is added to '0' (ascii 48), which is- if x is even, '0' + (x & 1) stays '0', otherwise it becomes '1' as x&1 returns 1 and '0'+1 is '1'. After that in "x >>= 1", ">>" is Bitwise right shift operator, which is equivalent to x = x / 2 or x /= 2. But there is little bit difference if we consider integers. Consider x = 12, that is 1100 in binary.

if we execute x >>= 1, then x becomes 6, if shifts away last 0 of 1100, becomes 110.
again if we execute x >>= 1, then x becomes 3, if shifts away last 0 of 110, becomes 11.
again if we execute x >>= 1, then x becomes 1, if shifts away last 1 of 11, becomes 1.
again if we execute x >>= 1, then x becomes 0, if shifts away last 1 of 1, becomes 0.

Finally, if x is even it stores '0' in buffer[index], otherwise stores '1', until x is not 0.

nim_10
  • 477
  • 8
  • 21