The flag used has only the MSB set and all other bits are cleared, so when you are bitwise and with a number, you can check the MSB in the number.
There are two results of bitwise handling:
- Zero - means the number is 0 MSB.
- Non-Zero - means that the number was 1 in his MSB.
Now we need a way to display
Non-zero -> 1 Zero -> 0
therefore we use double negation.
The same could be done using:
for(i=0;i<sizeof(int)*8;i++) { (n & flag) ? printf("1"):printf("0"); n = n << 1; }
codaddict
source share