>, and ~. It provides examples of their usage and outputs. It notes that bitwise operators work at the bit level on numbers. Some interesting facts mentioned are that XOR is useful for problems like finding the odd occurring number in a set, left and right shifts act as multiplication and division by 2 for positive numbers, and & can check if a number is odd or even. Care must be taken with ~ and with negative numbers for shifts. Bitwise operators are not the same as logical operators.">>, and ~. It provides examples of their usage and outputs. It notes that bitwise operators work at the bit level on numbers. Some interesting facts mentioned are that XOR is useful for problems like finding the odd occurring number in a set, left and right shifts act as multiplication and division by 2 for positive numbers, and & can check if a number is odd or even. Care must be taken with ~ and with negative numbers for shifts. Bitwise operators are not the same as logical operators.">
Interesting Facts About Bitwise Operators in C - GeeksforGeeks
Interesting Facts About Bitwise Operators in C - GeeksforGeeks
Interesting Facts About Bitwise Operators in C - GeeksforGeeks
& (bitwise AND) Takes two numbers as operand and does AND on every bit of two numbers. The result
of AND is 1 only if both bits are 1.
| (bitwise OR) Takes two numbers as operand and does OR on every bit of two numbers. The result of
OR is 1 any of the two bits is 1.
^ (bitwise XOR) Takes two numbers as operand and does XOR on every bit of two numbers. The result
of XOR is 1 if the two bits are different.
<< (left shift) Takes two numbers, left shifts the bits of first operand, the second operand decides the
number of places to shift.
>> (right shift) Takes two numbers, right shifts the bits of first operand, the second operand decides
the number of places to shift.
http://www.geeksforgeeks.org/interesting-facts-bitwise-operators-c/ 1/6
7/15/2017 Interesting Facts about Bitwise Operators in C - GeeksforGeeks
return 0;
}
Output:
a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18
b>>1 = 4
1) The left shift and right shift operators should not be used for negative numbers The result of
<< and >> is undefined behabiour if any of the operands is a negative number. For example results of
both -1 << 1 and 1 << -1 is undefined. Also, if the number is shifted more than the size of integer, the
behaviour is undefined. For example, 1 << 33 is undefined if integers are stored using 32 bits. See this
for more details.
2) The bitwise XOR operator is the most useful operator from technical interview perspective. It
is used in many problems. A simple example could be “Given a set of numbers where all elements
occur even number of times except one number, find the odd occuring number” This problem can be
efficiently solved by just doing XOR of all numbers.
int main(void) {
int arr[] = {12, 12, 14, 90, 14, 14, 14};
int n = sizeof(arr)/sizeof(arr[0]);
printf ("The odd occurring element is %d ", findOdd(arr, n));
return 0;
}
// Output: The odd occurring element is 90
The following are many other interesting problems which can be used using XOR operator.
Find the Missing Number, swap two numbers without using a temporary variable, A Memory Efficient
Doubly Linked List, and Find the two non-repeating elements. There are many more (See this, this, this,
▲
this, this and this)
http://www.geeksforgeeks.org/interesting-facts-bitwise-operators-c/ 2/6
7/15/2017 Interesting Facts about Bitwise Operators in C - GeeksforGeeks
int main()
{
int x = 2, y = 5;
(x & y)? printf("True ") : printf("False ");
(x && y)? printf("True ") : printf("False ");
return 0;
}
// Output: False True
4) The left-shift and right-shift operators are equivalent to multiplication and division by 2
respectively.
As mentioned in point 1, it works only if numbers are positive.
int main()
{
int x = 19;
printf ("x << 1 = %d\n", x << 1);
printf ("x >> 1 = %d\n", x >> 1);
return 0;
}
// Output: 38 9
5) The & operator can be used to quickly check if a number is odd or even
The value of expression (x & 1) would be non-zero only if x is odd, otherwise the value would be zero.
int main()
{
int x = 19;
(x & 1)? printf("Odd"): printf("Even");
return 0;
}
// Output: Odd
Please write comments if you find anything incorrect, or you want to share more information about the
topic discussed above
Recommended Posts:
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
@geeksforgeeks, Some rights reserved Contact Us! About Us! Careers! Privacy Policy
▲
http://www.geeksforgeeks.org/interesting-facts-bitwise-operators-c/ 4/6
7/15/2017 Interesting Facts about Bitwise Operators in C - GeeksforGeeks
http://www.geeksforgeeks.org/interesting-facts-bitwise-operators-c/ 5/6
7/15/2017 Interesting Facts about Bitwise Operators in C - GeeksforGeeks
http://www.geeksforgeeks.org/interesting-facts-bitwise-operators-c/ 6/6