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++){
putchar(value&displayMask ? '1' : '0');
value = value << 1;
if(c%8 == 0){
putchar(' ');
}
}
putchar('\n');
}