Output of C programs | Set 55 (Shift Operators)

Prerequisite: Shift operators

Q.1 What Is The Output Of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    unsigned int i = 0x80;
    printf("%d ", i << 1);
    return 0;
}

chevron_right


Option
a) 0
b) 256
c) 100
d) 80



ans :- b

Explanation :- 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?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    unsigned int a = 0xffff;
    unsigned int k = ~a;
    printf("%d %d\n", a, k);
    return 0;
}

chevron_right


Option
a) 65535 -65536
b) -65535 65535
c) 65535 65535
d) -65535 -65535

ans :- a

Explanation :- 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 65535.

Q.3 What Is The Output Of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    unsigned int a = -1;
    unsigned int b = 4;
    printf("%d\n", a << b);
    return 0;
}

chevron_right


Option
a) -1
b) 16
c) 8
d) -16

ans :- d

Explanation :- Here a integer is simply left-shifted 4 bit and print so it is -16.

Q.4 What Is The Output Of this program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    unsigned int m = 32;
    printf("%d %d", m >> 1, ~m);
    return 0;
}

chevron_right


Option
a) 16 5
b) 64 -32
c) 16 33
d) 16 -33

ans :- d

Explanation :- 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?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    unsigned int a = -1;
    unsigned int b = 15;
    printf("%d, ", a << 1);
    printf("%d\n", b >> 1);
    return 0;
}

chevron_right


Option
a) -2, 7
b) 2, 8
c) -1, 15
d) -2, 15

ans :- a

Explanation :- 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.

This article is contributed by Gyayak jain . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Sanjay Jeyavel, shi143din



Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.