Docsity

Bitwise operating system in c programming, Schemes and Mind Maps of C programming

Toggle sidebar
bg1
Bitwaise operator in c
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.
Types of Bitwise Operators in C
There are various types of bitwise operators used in all the programming languages. Here is a list of the
ones used in C:
• Bitwise OR Operator
• Bitwise AND Operator
• Unary Operator (Binary One’s complement operator)
• Bitwise XOR Operator
• Binary Right Shift Operator
• Binary Left Shift Operator
The table below lists all the Bitwise operations that are supported by the C language. Let us assume that
the variable ‘Q’ holds 13 while a variable ‘P’ holds 60, then:
Operator
Meaning
Description
Examples
|
Bitwise OR operator
It copies a bit when it exists in either of the
operands.
(P | Q) = 61, which
is, 0011 1101
&
Bitwise AND
operator
It copies a bit to the result when it exists in both
the operands.
(P & Q) = 12, which
is, 0000 1100
~
Binary Ones
complement operator
It is a unary operator that has an effect to ‘flip’ the
bits. Meaning, all the 0s become 1s and vice-versa.
(~P ) = ~(60), which
is,. 1100 0011
^
Bitwise XOR
operator
It is an exclusive OR operator that copies the bit
when it is set in one of the operands, but not in
both.
(P | Q) = 61, which
is, 0011 1101
>>
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
Example of Bitwise Operators in C
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 */
pf3
pf4

Partial preview of the text

Download Bitwise operating system in c programming and more Schemes and Mind Maps C programming in PDF only on Docsity!

Bitwaise operator in c

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.

Types of Bitwise Operators in C

There are various types of bitwise operators used in all the programming languages. Here is a list of the ones used in C:

  • Bitwise OR Operator
  • Bitwise AND Operator
  • Unary Operator (Binary One’s complement operator)
  • Bitwise XOR Operator
  • Binary Right Shift Operator
  • Binary Left Shift Operator The table below lists all the Bitwise operations that are supported by the C language. Let us assume that the variable ‘Q’ holds 13 while a variable ‘P’ holds 60, then: Operator Meaning Description Examples | Bitwise OR operator It copies a bit when it exists in either of the operands. (P | Q) = 61, which is, 0011 1101 & Bitwise AND operator It copies a bit to the result when it exists in both the operands. (P & Q) = 12, which is, 0000 1100 ~ Binary Ones complement operator It is a unary operator that has an effect to ‘flip’ the bits. Meaning, all the 0s become 1s and vice-versa. (~P ) = ~(60), which is,. 1100 0011 ^ Bitwise XOR operator It is an exclusive OR operator that copies the bit when it is set in one of the operands, but not in both. (P | Q) = 61, which is, 0011 1101

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

Example of Bitwise Operators in C

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.