0% found this document useful (0 votes)
6 views

Bitwise

Uploaded by

tempokpmet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Bitwise

Uploaded by

tempokpmet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Bitwise Operators in C/C++

1. The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two
numbers. The result of AND is 1 only if both bits are 1.
2. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two num
The result of OR is 1 if any of the two bits is 1.
3. The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two
numbers. The result of XOR is 1 if the two bits are different.
4. The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second
operand decides the number of places to shift.
5. The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, the second
operand decides the number of places to shift.
6. The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.
de <iostream>
namespace std;

in() {
// a = 5(00000101), b = 9(00001001)
int a = 5, b = 9;

// The result is 00000001


cout<<"a = " << a <<","<< " b = " << b <<endl;
cout << "a & b = " << (a & b) << endl;

// The result is 00001101


cout << "a | b = " << (a | b) << endl;

// The result is 00001100


cout << "a ^ b = " << (a ^ b) << endl;

// The result is 11111010


cout << "~a = " << (~a) << endl;

// The result is 00010010


cout<<"b << 1" <<" = "<< (b << 1) <<endl;

// The result is 00000100


cout<<"b >> 1 "<<"= " << (b >> 1 )<<endl;

return 0;

b=9
=1
= 13
= 12
dd occurring element is 90
Complexity: O(n)
ary Space: O(1)

1. Find the Missing Number


2. swap two numbers without using a temporary variable
3. A Memory Efficient Doubly Linked List
4. Find the two non-repeating elements.
5. Find the two numbers with odd occurrences in an unsorted-array.
6. Add two numbers without using arithmetic operators.
7. Swap bits in a given number.
8. Count number of bits to be flipped to convert a to b .
9. Find the element that appears once.
10. Detect if two integers have opposite signs.
itwise operators should not be used in place of logical operators. The result of logical operators (&&,
is either 0 or 1, but bitwise operators return an integer value. Also, the logical operators consider any non
nd as 1. For example, consider the following program, the results of & and && are different for same opera

de <iostream>
namespace std;

in()

int x = 2, y = 5;
(x & y) ? cout <<"True " : cout <<"False ";
(x && y) ? cout <<"True " : cout <<"False ";
return 0;

True
Complexity: O(1)
ary Space: O(1)
eft-shift and right-shift operators are equivalent to multiplication and division by 2 respectively. As
oned in point 1, it works only if numbers are positive.

de <iostream>

namespace std;

in() {
operator should be used carefully. The result of ~ operator on a small number can be a big number if t
is stored in an unsigned variable. And the result may be a negative number if the result is stored in a sign
le (assuming that the negative numbers are stored in 2’s complement form where the leftmost bit is the si

ude <iostream>

namespace std;

in() {

unsigned int x = 1;

signed int a = 1;

cout<<"Signed Result "<< ~a <<endl ;

cout<<"Unsigned Result "<< ~x ;

return 0;

d Result -2
ned Result 4294967294
Complexity: O(1)
ary Space: O(1)

You might also like