1. ## A Problem About Usage of Bitwise Operator &

I can't solve how the program works lowermost. Maybe, I could it, but I may not notice to know the true solution, or I'm not sure.

For example, I entered number 12, after I ran the program:

Number 12 is 00000000 00000000 00000000 00001100.
displayMask is 10000000 00000000 00000000 00000000.

I can see that expression inside for statement.
Code:
```        putchar(value&displayMask ? '1' : '0');
value = value << 1;```
I know that;
1 & 0 = 0
1 & 1 = 1
0 & 0 = 0
0 & 1 = 0.

But, there isn't just a single number, and isn't just a single comparison in the program. There are 32 numbers and comparisons.(According to my studying book, it says unsigned integers are always stored in 32bits(4bytes) of memory.)

Question 1: Does the compiler implement processing operator & between 12 and 10000000 00000000 00000000 00000000?

Question 2:Or does it implement this processing between 00000000 00000000 00000000 00001100 and 10000000 00000000 00000000 00000000?

Question 1.a: If you say yes for question1, then how can be handled this processing with the operator &? (12 & 100000000 ...)

Question 2.a: If you say no for question1 and yes for question2, then how can be handled the comparisons of the all numbers?(Meanwhile, if you say yes for question2, then it means that the compiler has already known 'its value in binary system' of '12 in decimal system' ?)

Question 3: Does the compiler know that it needs binary base of unsigned integer numbers to comparison variables with & ? If yes, so how can we print to output situtation in binary system of any numbers by avoiding indirect way as the expression putchar(value&displayMask ? '1' : '0'); ?

Code:
```#include <stdio.h>
#include <stdlib.h>

void displayBits(unsigned);

int main()
{
unsigned x;

printf("Enter a value: ");
scanf("%u",&x);

displayBits(x);

return 0;
}

void displayBits(unsigned value)
{
unsigned c;
unsigned displayMask = 1 << 31;

printf("\n%10u = ",value);

for(c=1;c<=32;c++){
value = value << 1;

if(c%8 == 0){
putchar(' ');
}
}
putchar('\n');
}```

2. Originally Posted by hefese
But, there isn't just a single number, and isn't just a single comparison in the program. There are 32 numbers and comparisons.
If you want to understand the bitwise and of two 32-bit integers as the and operation applied to each of the corresponding 32 bits, then there are 32 operations (or comparisons). However, the fact remains that this is just one operation involving two integer values.

Originally Posted by hefese
According to my studying book, it says unsigned integers are always stored in 32bits(4bytes) of memory.
Your book is not correct. An "unsigned integer" can mean one of several different unsigned integer types (e.g., unsigned int, unsigned long), and they are not required to be exactly 32 bits in size.

Originally Posted by hefese
Question 1: Does the compiler implement processing operator & between 12 and 10000000 00000000 00000000 00000000?

Question 2:Or does it implement this processing between 00000000 00000000 00000000 00001100 and 10000000 00000000 00000000 00000000?

Question 1.a: If you say yes for question1, then how can be handled this processing with the operator &? (12 & 100000000 ...)

Question 2.a: If you say no for question1 and yes for question2, then how can be handled the comparisons of the all numbers?(Meanwhile, if you say yes for question2, then it means that the compiler has already known 'its value in binary system' of '12 in decimal system' ?)
I don't understand what you mean by the above questions. The compiler translates the operations in C to assembly code (or machine code, or first to some intermediate language). For an unsigned integer, 12 and 00000000 00000000 00000000 00001100 are exactly the same thing, except for representation.

Originally Posted by hefese
Question 3: Does the compiler know that it needs binary base of unsigned integer numbers to comparison variables with & ?
Yes, in the sense that this is how bitwise and is defined. It is bitwise, and a bit is a "digit" in binary.

Originally Posted by hefese
If yes, so how can we print to output situtation in binary system of any numbers by avoiding indirect way as the expression putchar(value&displayMask ? '1' : '0'); ?
There is no standard format specifier for printf that will format in binary, so this "indirect way" is the way to go.

3. Originally Posted by laserlight
If you want to understand the bitwise and of two 32-bit integers as the and operation applied to each of the corresponding 32 bits, then there are 32 operations (or comparisons). However, the fact remains that this is just one operation involving two integer values.
I still don't understand that statement putchar prints just one digit(bit) at once, although there are 32 results due to value&displayMask.

00000000 00000000 00000000 00001100 = > 32 digits
10000000 00000000 00000000 00000000 = > 32 digits
&------------------------------------------- => 32 comparisons
00000000 00000000 00000000 00000000 = > 32 results.

I could not mean to properly,clearly at the earlier post. There are 32 results. But it is printed as one bit with putchar statement once. I guess the compiler ignores 31 results each time. But How does the compiler decide to choose which results that putchar prints one bit each time?

Originally Posted by laserlight
I don't understand what you mean by the above questions. The compiler translates the operations in C to assembly code (or machine code, or first to some intermediate language). For an unsigned integer, 12 and 00000000 00000000 00000000 00001100 are exactly the same thing, except for representation.

Originally Posted by laserlight
There is no standard format specifier for printf that will format in binary, so this "indirect way" is the way to go.
OK.

4. I think you're misunderstanding what the code is actually doing. The actual result of the & statement is a single result; i.e.

Code:
```Example with decimal addition:

123  // three digits
321  // three digits
444  // one result```
The code is using a shifting technique to check the value of each binary place and print out the corresponding bit value. It compares against a fixed value with the MSB set. The value under test is shifted (and the result printed each iteration) to produce the binary representation. It's not a direct binary print, but just a clever way of producing equivalent results.

Just take the code step by step. If you can follow this example, you'll understand how the "putchar()" statement in your code is working:

Code:
```Lets assume one byte for simplicity:

Value =  12 = 0x0C

----------

Run 1:

// putchar(value&displayMask ? '1' : '0');

Value: 0000 1100
&      ---------
0000 0000 = false = 0 (print this value)

Run 2:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0001 1000
&      ---------
0000 0000 = false = 0 (print this value)

Run 3:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0011 0000
&      ---------
0000 0000 = false = 0 (print this value)

Run 4:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0110 0000
&      ---------
0000 0000 = false = 0 (print this value)

Run 5:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 1100 0000
&      ---------
1000 0000 = true = 1 (print this value)

Run 6:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 1000 0000
&      ---------
1000 0000 = true = 1 (print this value)

Run 7:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0000 0000
&      ---------
0000 0000 = false = 0 (print this value)

Run 8:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0000 0000
&      ---------
0000 0000 = false = 0 (print this value)

// End result = 0000 1100 = 0x0C = 12```

5. displayMask has only one bit set in it on purpose, it's so that the rest of the bits are ignored (set to zero) as far as the rsult is concerned. Thus each of the 32 results hinges solely on the bit from value in the same position. You'll note that the bits in value shift along to the left by one place each time through the loop as well. This is critical so that a different bit is in the same place as the bit in displayMask each time.
Make sense?

6. Originally Posted by Matticus
I think you're misunderstanding what the code is actually doing. The actual result of the & statement is a single result; i.e.

Code:
```Example with decimal addition:

123  // three digits
321  // three digits
444  // one result```
The code is using a shifting technique to check the value of each binary place and print out the corresponding bit value. It compares against a fixed value with the MSB set. The value under test is shifted (and the result printed each iteration) to produce the binary representation. It's not a direct binary print, but just a clever way of producing equivalent results.

Just take the code step by step. If you can follow this example, you'll understand how the "putchar()" statement in your code is working:

Code:
```Lets assume one byte for simplicity:

Value =  12 = 0x0C

----------

Run 1:

// putchar(value&displayMask ? '1' : '0');

Value: 0000 1100
&      ---------
0000 0000 = false = 0 (print this value)

Run 2:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0001 1000
&      ---------
0000 0000 = false = 0 (print this value)

Run 3:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0011 0000
&      ---------
0000 0000 = false = 0 (print this value)

Run 4:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0110 0000
&      ---------
0000 0000 = false = 0 (print this value)

Run 5:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 1100 0000
&      ---------
1000 0000 = true = 1 (print this value)

Run 6:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 1000 0000
&      ---------
1000 0000 = true = 1 (print this value)

Run 7:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0000 0000
&      ---------
0000 0000 = false = 0 (print this value)

Run 8:

// value = value << 1;
// putchar(value&displayMask ? '1' : '0');

Value: 0000 0000
&      ---------
0000 0000 = false = 0 (print this value)

// End result = 0000 1100 = 0x0C = 12```
When I read your posts, I noticed that I didn't read laserlight's posts enough well. Thank you Matticus for representation of the processing &. It was very helpful for me to understand.

Originally Posted by iMalc
displayMask has only one bit set in it on purpose, it's so that the rest of the bits are ignored (set to zero) as far as the rsult is concerned. Thus each of the 32 results hinges solely on the bit from value in the same position. You'll note that the bits in value shift along to the left by one place each time through the loop as well. This is critical so that a different bit is in the same place as the bit in displayMask each time.
Make sense?
I guess so. Thank you iMalc for your explanation.