Output of C++ programs | Set 48 (Bit Manipulation) Last Updated : 22 Sep, 2017 Comments Improve Suggest changes Like Article Like Report Q.1 What Is The Output Of this program ? CPP #include <iostream> 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 ? CPP #include <iostream> 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 ? CPP #include <iostream> 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 ? CPP #include <iostream> 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 ? CPP #include <iostream> 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. Comment More infoAdvertise with us Next Article Implementation of C++ Bitset using String G Gyayak Jain Improve Article Tags : C++ CPP-Output Practice Tags : CPP Similar Reads Output of C++ programs | Set 45 Q.1 What Is The Output Of this program? CPP #include <iostream> using namespace std; int main() { int a = b = c = 10; a = b = c = 50; printf("%d %d %d", a, b, c); return 0; } Option a) 50 50 50 b) Three Garbage Value c) 10 10 10 d) Compile Time Error Ans: d Explanation : In this prog 2 min read Implementation of C++ Bitset using String Let's implement bitset in C++, such that following operations can be performed in stated time complexities : init(int size): initializes a bitset of size number of 0 bits.void fix(int pos): Change the bit at position pos to 1. No change if it was already 1.void unfix(int pos): Change the bit at posi 4 min read bitset operator[] in C++ STL bitset::operator[] is a built-in function in C++ STL which is used to assign value to any index of a bitset. Syntax: bitset_operator[index] = value Parameter: The parameter index specifies the position at which the value is to be assigned. Return Value: The function returns the value to the bit at t 2 min read bitset reset() function in C++ STL bitset::reset() is a built-in function in C++ STL which resets the bits at the given index in the parameter. If no parameter is passed then all the bits are reset to zero. Syntax: reset(int index) Parameter: The function accepts a parameter index which signifies the position at which the bit has to 2 min read bitset reset() function in C++ STL bitset::reset() is a built-in function in C++ STL which resets the bits at the given index in the parameter. If no parameter is passed then all the bits are reset to zero. Syntax: reset(int index) Parameter: The function accepts a parameter index which signifies the position at which the bit has to 2 min read bitset set() function in C++ STL bitset::set() is a built-in STL in C++ which sets the bit to a given value at a particular index. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that particular index to 1. Syntax: set(int index, bool val) Parameter: The function accepts tw 2 min read Like