retagged by
451 views

1 Answer

Best answer
2 votes
2 votes

your program will print 16 bit binary equivalent.

let,instead of  "0 to 15" , for loop iterates from "0 to 3" to print 4 bit binary equivalent.

then working is as given below:

num=2 . binary equivalent =0010

iteration 1: i=0

2<<0 & 1<<3 means left shift 2 by 0 and 1 by 3 and then perform bitwise and(&) of both

as 2<<0=0010 and 1<<3= 1000 therefore ending both will give us 0000 therefore 0 will be printed according to condition,

iteration 2: i=1

2<<1&1<<3=0100&1000=0000, 0 will be printed

iteration 3: i=2

2<<2&1<<3= 1000&1000=1000 (non zero), 1 will be printed

iteration 4:i=3

2<<3&1<<3=0000& 1000 =0000 , zero fill be printed

therefore final result on screen  will be 0010

similarly u can analyze for 16 bit output.

LOGIC OF PROGRAM:

extract one bit from left side of binary representation of given number by left shifting and then check whether it is 1 or 0 by ending it with 1 and print the result of ending.

selected by

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 5 hours ago
16 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked 6 days ago
87 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
248 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how