


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of bitwise operators in C language. It describes the types of bitwise operators and their usage in numerical computations. The document also provides examples of how to use bitwise operators in C language. It explains the meaning and description of each operator with examples. useful for computer science students who are learning C programming language.
Typology: Schemes and Mind Maps
1 / 4
This page cannot be seen from the preview
Don't miss anything!



We use the bitwise operators in C language to perform operations on the available data at a bit level. Thus, performing a bitwise operation is also called bit-level programming. It is mainly used in numerical computations for a faster calculation because it consists of two digits – 1 or 0.
There are various types of bitwise operators used in all the programming languages. Here is a list of the ones used in C:
Shift operator (Right) It moves the value of the left operand to the right by the number of bits that the right operand specifies. P >> 2 = 15 which is, 0000 1111 << Shift operator (Left) It moves the value of the right operand to the left by the number of bits that the right operand specifies. P << 2 = 240 which is, 1111 0000
Let us look at the following example to understand how the bitwise operators work in the C language: #include <stdio.h> main() { unsigned int p = 60; /* 60 = 0011 1100 / unsigned int q = 13; / 13 = 0000 1101 */
int r = 0; r = p | q; /* 61 = 0011 1101 / printf(“Line 1 – The value of r is %d\n”, r ); r = p & q; / 12 = 0000 1100 / printf(“Line 2 – The value of r is %d\n”, r ); r = ~p; /-61 = 1100 0011 / printf(“Line 3 – The value of r is %d\n”, r ); r = p ^ q; / 49 = 0011 0001 / printf(“Line 4 – The value of r is %d\n”, r ); r = p >> 2; / 15 = 0000 1111 / printf(“Line 5 – The value of r is %d\n”, r ); r = p << 2; / 240 = 1111 0000 */ printf(“Line 6 – The value of r is %d\n”, r ); } The compilation and execution of the program mentioned above will produce the result as − Line 1 – The value of c is 61 Line 2 – The value of c is 12 Line 3 – The value of c is - 61 Line 4 – The value of c is 49 Line 5 – The value of c is 15 Line 6 – The value of c is 240 Here is another example regarding how we can use the bitwise operators in the C language: // C Program to demonstrate use of bitwise operators #include <stdio.h> int main() { // p = 5(00000101), q = 9(00001001) unsigned char p = 5, q = 9; // The result is 00000001 printf(“p = %d, q = %d\n”, p, q); printf(“p&q = %d\n”, p & q); // The result is 00001100 printf(“p^q = %d\n”, p ^ q); // The result is 00001101 printf(“p|q = %d\n”, p | q); // The result is 11111010 printf(“~p = %d\n”, p = ~p); // The result is 00000100 printf(“q>>1 = %d\n”, q >> 1);
The generated output would be: False True
5. The right-shift and left-shift operators are equivalent to respectively division and multiplication by 2. But it will work only when the available numbers are positive. Let us look at an example, #include <stdio.h> int main() { int a = 19; printf(“a >> 1 = %d\n”, a >> 1); printf(“a << 1 = %d\n”, a << 1); return 0; } The generated output would be: a >> 1 = 9 a << 1 = 38 6. We can use the & operator to check quickly if a number is even or odd – The value of the given expression would be non-zero (x & 1) if x is odd. In other cases, the value will be zero. Let us look at an example, #include <stdio.h> int main() { int x = 19; (x & 1)? printf(“Even”) : printf(“Odd”); return 0; } The generated output would be: Even 7. We have to use the ~ operator very carefully – The result obtained from the ~ operator on any small number could be a big number in case we store the result in an unsigned variable. Also, the result can be a negative number when this result is stored in a variable that is signed.