Output of C++ programs | Set 48 (Bit Manipulation)

Q.1 What Is The Output Of this program ?

 #include using namespace std; int main() {     int a = 35;     int b = 12;     printf("%d ", ~a);     printf("%d ", ~ - b);     return 0; }

Option
a) -36 11
b) -35 -12
c) 220 11
d) 36 11

ans :-a

Explanation:- For any integer n, bitwise complement of n always will be -(n+1). so here -(35+1) and-(-12+1).

Q.2 What Is The Output Of this program ?

 #include using namespace std; int main() {     int a = 12;     int b = 25;     int c = 1;     printf("%d", a ^ b && c);     return 0; }

Option
a) 300
b) 1
c) 25
d) 212

ans :-b

Explanation:- In This program, we simple perform a XOR operation between 12(1010) and 25(11001) so result is(10011) equal to the 21, then perform and operation.

Q.3 What Is The Output Of this program ?

 #include using namespace std; int main() {     unsigned char a = -8;     unsigned char b = a >> 1;     printf("%d\n", b); }

Option
a) 128
b) -8
c) -4
d) 124

ans :-d

Explanation:- We know that Negative numbers are represented using 2’s complement of its positive equivalent. So the 2’s complement of 8 is (11111000) and we shifted its 1 so its (01111100) is 124.

Q.4 What Is The Output Of this program ?

 #include using namespace std; int main() {     signed char a = -128;     signed char b = a >> 1;     printf("%d\n", b);     return 0; }

Option c
a) 128
b) 256
c) -64
d) 64

ans :- -64

Explanation:- In this code, we are right shifting -128 by 1. The result will be “-64”, binary shift is applied since the operand is a signed value are printed.

Q.5 What Is The Output Of this program ?

 #include using namespace std; int main() {     unsigned int x = 3;     x = x | (x << x);     cout << x;     return 0; }

Option
a) 27
b) 24
c) 1
d) 0

ans :-a

Explanation:- In this program integer a is 3 its binary (011) shifted 3 its(011000) equal to the 24 and add 3 so its equal to the 27.

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

Article Tags :

Be the First to upvote.

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