I came across this code for the binary representation of a number. I want to know the need for using !! in the code.
int main() {
int n,i;
unsigned flag = 1<<(sizeof(int) * 8 - 1);
printf("Input the number\n");
scanf("%d",&n);
for(i=0;i<sizeof(int)*8;i++) {
printf("%d",!!(n & flag) );
n = n << 1;
}
return 0;
}
The flag used has only the MSB set and all other bits cleared, so that when you
bitwise andit with number you can test the MSB in the number.There are two outcomes of the bitwise anding:
MSB.
its MSB.
Now we need a way to map
so we use the double negation.
The same thing could have been done using: