- 1
Explain the output
#include <stdio.h> #include <stdlib.h> int main() { char c=48 ; int i=0,mask=01 ; for(i=0;i<=5;i++){ printf("%c\n",c|mask); mask=mask<<1 ; } return 0; }
1 Answer
+ 10
You start with c = 48, which is hex 30 or binary 00110000. Showing the numbers in binary, after i == 4, then mask = 00010000 and c | mask = 00110000 | 00010000 = 00110000 (no change since the bit is already set). The same thing happens for the next step where mask = 00100000.
so c=00110000 for i =1 c=00110001=49 and the assci code of 49 is 1 so it print 1
c=00110000 for i=2 c=00110010=50 and the assci code of 50 is 4 so print 4
c=00110000 for i=3 mask c=00110100=52 and the ascii code of 52 is 8 so print 8
c=00110000 for i=4 mask c=00111000=56 and the ascii code of 56 is 0 so print 0
and So output is 1 2 4 8 0 0