Prerequisite: Shift operators
Q.1 What Is The Output Of this program?
C
Option
a) 0
b) 256
c) 100
d) 80
C
Option
a) 65535 -65536
b) -65535 65535
c) 65535 65535
d) -65535 -65535
C
Option
a) -1
b) 16
c) 8
d) -16
C
Option
a) 16 5
b) 64 -32
c) 16 33
d) 16 -33
C
#include <stdio.h>
int main()
{
unsigned int i = 0x80;
printf("%d ", i << 1);
return 0;
}
ans :- bExplanation :- We know that 0x is hexa-decimal representation of number so 80 converted in decimal is 128 binary(10000000), its left shift 1 so it is (100000000)equal to 256. Q.2 What Is The Output Of this program?
#include <stdio.h>
int main()
{
unsigned int a = 0xffff;
unsigned int k = ~a;
printf("%d %d\n", a, k);
return 0;
}
ans :- aExplanation :- This code 0x is Hexadecimal representation of number so ffff hexadecimal is converted into Decimal 65535 and stored in a. The negation of a -65535 is stored in k. k is an unsigned integer but it has a negative value in it. When k is being printed the format specifier is %d which represents signed integer thus -65536 gets printed as value of k. If the format specifier for k had been %u the value that would have been printed is 4294901760. Q.3 What Is The Output Of this program?
#include <stdio.h>
int main()
{
unsigned int a = -1;
unsigned int b = 4;
printf("%d\n", a << b);
return 0;
}
ans :- dExplanation :- Here a integer is simply left-shifted 4 bit and print so it is -16. Q.4 What Is The Output Of this program?
#include <stdio.h>
int main()
{
unsigned int m = 32;
printf("%d %d", m >> 1, ~m);
return 0;
}
ans :- dExplanation :- This program 32 is(10000) and we know that negative number in binary system contain a one's complement so here take a complement of a number and only print this number so the negation of 32 is -33. Q.5 What Is The Output Of this program?
#include <stdio.h>
int main()
{
unsigned int a = -1;
unsigned int b = 15;
printf("%d, ", a << 1);
printf("%d\n", b >> 1);
return 0;
}
Option
a) -2, 7
b) 2, 8
c) -1, 15
d) -2, 15
ans :- aExplanation :- Here -1 binary(0001) if left shift 1 so it is -2(0010) and 15 binary(1111) is right shift 1 so it is (0111)so value 7.