C Programming :: Declarations and Initializations - Discussion
Home > C Programming > Declarations and Initializations > MCQs Questions Discussion
11 / 54
What does the following 'C' program do ?
#include<stdio.h>
void main()
{
unsigned int num;
int i;
scanf("%u", &num);
for (i = 0; i < 16; i++)
printf("%d", (num << i & 1 << 15)? 1:0);
}
#include<stdio.h>
void main()
{
unsigned int num;
int i;
scanf("%u", &num);
for (i = 0; i < 16; i++)
printf("%d", (num << i & 1 << 15)? 1:0);
}
AIt prints all even bits form num
BIt prints binary equivalent of num
CIt prints all odd bits from num
DNone of these
Answer: Option (Login/Signup)
Show Explanation
Asked In ::
STEP-BY-STEP
Here integer (unsigned) is assumed as 2 bytes so that for loop is till 16 i.e (2byte = 16 bits.)
As unsigned integer so user is expected to enter positive number.
statement in printf (num << i & 1 << 15)? 1:0 is used to convert decimal to binary equivalent.
in the above statement below things has been used.
<< ----> left shift
& -----> And operator
? : ------> trinery operator
Ex: if the number is 10 and its binary equivalent 1010 or (00000000000001010 in 16 bit)
the printf statement will be
for i = 0
(10 << 0 & 1 << 15)>?1:0
for i = 1
(10 << 1 & 1 << 15)>?1:0
similarly for i = 2, 3, 4 , ---- 15
As unsigned integer so user is expected to enter positive number.
statement in printf (num << i & 1 << 15)? 1:0 is used to convert decimal to binary equivalent.
in the above statement below things has been used.
<< ----> left shift
& -----> And operator
? : ------> trinery operator
Ex: if the number is 10 and its binary equivalent 1010 or (00000000000001010 in 16 bit)
the printf statement will be
for i = 0
(10 << 0 & 1 << 15)>?1:0
for i = 1
(10 << 1 & 1 << 15)>?1:0
similarly for i = 2, 3, 4 , ---- 15
Read Full Answer
Report Error
Please Login First Click Here