Clouds
Home
Tutorial of C language
Computer Language
Introduction to C
Programming in C
Structure of C program
Variable in C
Datatypes in C
Token in C
Operator in C
Precedence of C Operator
Assignment Operator in C
Logical Operator in C
Bitwise Operator in C
Ternary Operator in C
C Input Output
Pointers in C
Storage Classes of C
Control Structure of C
C Decision Making Statement
C Looping Statement
C Jumping Statement
C Function
Function Call By Value
Function Call By Reference
C Input Output Function
C Structure
Array in C
Union in C

Bitwise Operators

C has the ability to support the manipulation of data at the bit level. Bitwise operators areused for operations on bits. Normally, most of the higher level programming languages do not support operations at bit level.

Note: -Bitwise operators are operated on only integers and Character but not on floats and dobles.

Bitwise Operator Meaning
& Bitwise AND
| Bitwise OR
~ Ones Complement
<< Left Shift
>> Right Shift
^ Bitwise XOR

Bitwise AND (&)

Bitwise AND (&) operator ia a binary operator which takes two operands of int/char type and performs bitwise AND operation. It is different than logical AND (&&) operator. Truth table for Bitwise AND (&) operator
First Bit Second Bit Resultant Bit
0 0 0
0 1 0
1 0 0
1 1 1
Example:-
				#include<stdio.h>
				#include<conio.h>
				void  main()
				{
				 	int a=9,b=3,c;
					clrscr();
					c=a & b;
					printf("the value of c=%d",c);
					getch();
				}
				

Output:C=1 Explanation:-
  • a=9= 0000 0000 0000 1001
  • b=3= 0000 0000 0000 0011
  • a&b= 0000 0000 0000 0001 (which is equivalent to 1 in decimal)

    Bitwise OR ( | )

    Bitwise OR ( | )operator is a binary operator which takes two operands of int/char type and performs bitwise OR operation. Ittakes two bits as operand and returnsthe value ON(1) if at least one of the two bits is 1.if both the bits are 0 only then it returns the value OFF(0). Truth table for Bitwise OR ( | ) operator
    First Bit Second Bit Resultant Bit
    0 0 0
    0 1 1
    1 0 1
    1 1 1
    Example:-
    				#include<stdio.h>
    				#include<conio.h>
    				void  main()
    				{
    				 	int a=10,b=6,c;
    					clrscr();
    					c=a | b;
    					printf("the value of c=%d",c);
    					getch();
    				}
    				

    Output:C=14 Explanation:-
  • a=10= 0000 0000 0000 1010
  • b=6= 0000 0000 0000 0110
  • a | b= 0000 0000 0000 1110 (which is equivalent to 14 in decimal)

    Ones Compliment(~)

    ones Compliment(~) is a unary operator and compliments the bits of its operand that is all 1s are converted to 0s and all 0s areconverted to 1s. Example:-
    				#include<stdio.h>
    				#include<conio.h>
    				void  main()
    				{
    				 	int a=9,b;
    					clrscr();
    					b= ~a;
    					printf("the value of b=%d",b);
    					getch();
    				}
    				

    Output:b= -10

    Explanation:-

  • a=9= 0000 0000 0000 1001
  • 1111 1111 1111 0110 ( 1s Compliment) which is equivalent to 65526 i.e (-10) in integer range

    Left shift(<<) Operator

    Left shift (<<) operator is also unary operator i.e. it operator on a single operand. This operator shifts the bits of the operand towards left.the no of places the bits are shifted depends on the number following the operand. It means a<<3 would shift all bits three places towards left.As the bits are shifted to the left , blanks are created on the right. These blanks are always filled with 0s.

    Example:-


    				#include<stdio.h>
    				#include<conio.h>
    				void  main()
    				{
    				 int a=9,b;
    				clrscr();
    				b= a<<3;
    				printf("the value of b=%d",b);
    				getch();
    				}
    				

    Output:b= 72

    Explanation:-

  • a=9= 0000 0000 0000 1001
  • Shifting left 3 places
  • 0000 0000 0100 1000 (which is equivalent to 72 in decimal)
    Ternary Operator in C >>
    C Input Output >>
    Pointers in C >>
    Storage Classes of C >>
    Control Structure of C >>


    Home Clouds