Bitwise operations in C
Operations on bits at individual levels can be carried out using Bitwise operations in the C programming language.
Bits come together to form a byte which is the lowest form of data that can be accessed in digital hardwares. The whole representation of a number is considered while applying a bitwise operator.
Contents |
Bitwise operators[edit]
C provides six operators for bit manipulation.[1]
| Symbol | Operator |
|---|---|
| & | bitwise AND |
| l | bitwise inclusive OR |
| ^ | bitwise exclusive OR |
| << | left shift |
| >> | right shift |
| ~ | one's complement (unary) |
Bitwise AND "&"[edit]
| bit a | bit b | a & b (a AND b) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
The bitwise AND operator is a single ampersand: &. It is just a representation of AND and does its work on bits and not on bytes, chars, integers, etc. So basically a binary AND does the logical AND (as shown in the table above) of the bits in each position of a number in its binary form.
For instance, working with a byte (the char type):
11001110
& 10011000
= 10001000
The most significant bit of the first number is 1 and that of the second number is also 1 so we know the most significant bit of the result must be 1; in the second most significant bit, the bit of second number is zero, so we have the result as 0. [2]
Bitwise OR "|"[edit]
| bit a | bit b | a | b (a OR b) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Bitwise OR works in the same way as bitwise AND. Its result is a 1 if one of the either bits is 1 and zero only when both bits are 0. Its symbol is '|' which can be called a pipe.
11001110
| 10011000
= 11011110
Bitwise XOR "^"[edit]
| bit a | bit b | a ^ b (a XOR b) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
The Bitwise EX-OR (Exclusive Or) performs a logical EX-OR function or in simple term adds the two bits discarding the carry. Thus result is zero only when we have 2 zeroes or 2 ones to perform on. [4] Sometimes EX-OR might just be used to toggle the bits between 1 and 0. Thus: i = i ^ 1 when used in a loop toggles its values between 1 and 0. [5]
Summary of bitwise operators[edit]
| bit a | bit b | a & b (a AND b) | a | b (a OR b) | a ^ b (a XOR b) |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Shift Operators[edit]
There are two bitwise shift operators. They are:
- Right shift (>>)
- Left shift (<<)
Right shift >>[edit]
The symbol of right shift operator is >>. For its operation, it requires two operands. It shifts each bit in its left operand to the right. The number following the operator decides the number of places the bits are shifted (i.e. the right operand). Thus by doing ch >> 3 all the bits will be shifted to the right by three places and so on.
Example:
If the variable ch contains the bit pattern 11100101, then ch >> 1 will give the output as 01110010, and ch >> 2 will give 00111001.
Here blank spaces are generated simultaneously on the left when the bits are shifted to the right. When performed on an unsigned type, the operation performed is a logical shift, causing the blanks to be filled by 0s (zeros). When performed on a signed type, an arithmetic shift is performed, causing the blank to be filled with the sign bit of the left operand.
Right shift can be used to divide a bit pattern by 2 as shown:
i = 14; // Bit pattern 1110 j = i >> 1; // here we have the bit pattern shifted by 1 thus we get 111 = 7 which is 14/2
Implementation[edit]
The implementation of right shift operator in a C can be seen from the code written below.
Example:
#include<stdio.h>
void showbits(int);
int main() {
int j = 5225, m, n;
printf("The decimal %d is equal to binary-", j);
showbits(j); // assume we have a function that prints a binary string when given a decimal integer
// the loop for right shift operation
for ( m = 0; m <= 5; m++ )
{
n = j >> m;
printf("%d right shift %d gives ", j, m);
showbits(n);
}
return 0;
}
The output of above program will be:
The decimal 5225 is equal to binary-0001010001101001 5225 right shift 0 gives 0001010001101001 5225 right shift 1 gives 0000101000110100 5225 right shift 2 gives 0000010100011010 5225 right shift 3 gives 0000001010001101 5225 right shift 4 gives 0000000101000110 5225 right shift 5 gives 0000000010100011
<< Left shift[edit]
The symbol of left shift operator is <<. It shifts each bit in its left operand to the left. It works opposite to that of right shift operator. Thus by doing ch << 1 in the above example we have 11001010. Blank spaces generated are filled up by zeroes as above.
Left shift can be used to multiply an integer in multiples of 2 as in:
int i = 4 // bit pattern equivalent is 100 int j = i << 2 // makes it 10000 a multiple of 2 i.e 16
Unary Operator (~)/One's Complement[edit]
| bit a | ~a (complement of a) |
|---|---|
| 0 | 1 |
| 1 | 0 |
The one's complement (~) or the bitwise complement gets us the complement of a given number. Thus we get the bits inverted, for every bit 1 the result is bit 0 and conversely for every bit 0 we have a bit 1.
A Simple Addition Program[edit]
The following program adds 2 operands using AND, EX-OR and left shift (<<).
#include <stdio.h> int main() { unsigned int x=3, y=1, sum, carry; sum = x ^ y; // Ex - OR x and y carry = x & y; // AND x and y while(carry != 0) // enter the loop { carry = carry << 1; // left shift the carry x = sum; // initialize x as sum y = carry; // initialize y as carry sum = x ^ y; // sum is calculated carry = x & y; // carry is calculated and loop condition is checked and the process repeated until carry = 0. } printf("%d\n", sum); // the program will print 4 return 0; }
See also[edit]
References[edit]
- ^ Kernighan; Dennis M. Ritchie (March 1988). [[The C Programming Language (book)|The C Programming Language]] (2nd ed.). Englewood Cliffs, NJ: Prentice Hall. ISBN 0-13-110362-8. Wikilink embedded in URL title (help) Regarded by many to be the authoritative reference on C.
- ^ http://www.cprogramming.com/tutorial/bitwise_operators.html
- ^ http://www.cprogramming.com/tutorial/bitwise_operators.html
- ^ http://www.electronics-tutorials.ws/logic/logic_7.html
- ^ http://www.fredosaurus.com/notes-cpp/expressions/bitops.html