#include <stdio.h>
int main() {
unsigned short int num; // num is 16 bit unsigned integer
int i;
scanf("%u",&num);
for(i=0;i<16;i++) {
printf("%d",(num<<i & 1<<15)?1:0);
// ( (num<<i) & (1<<15) )?1:0
// << has higher precedence than &
}
}
// num << i == ith bit from MSB in the actual num is at the MSB position now
// 1 << 15 == 1000000000000000
// (num<<i)&(1<<15) check each bits of num starting from MSB and prints one by one
Therefore: The program prints binary equivalent of num.
Above task can also be performed using the following program:
#include <stdio.h>
int main() {
unsigned short int num;
int i;
scanf("%u",&num);
for(i=15;i>=0;i--)
(num&(1<<i))?printf("1"):printf("0");
}