Skip to main content

Bitwise Operators in C


Bit level operators make C useful for many low level applications. Bitwise operations allow one to read and manipulate bits in variables of certain types. The bit wise operators only work on int and char types.
The following are the bitwise operators in C

Operator
Description
~
Bitwise Complement
<< 
Bitwise Shift Left
>> 
Bitwise Shift Right
&
Bitwise AND
^
Bitwise EX-OR
|
Bitwise OR

Bitwise operations helps to work on Boolean variable, which needs only one bit.Set membership can be represented with Boolean variables ( 1 means element in the set 0 means not in the set) and the Bitwise-AND can be used to implement set-intersection and Bitwise-OR to implement set union.

The bitwise AND(&) is true only if both bits are set. The bitwise OR(|) is false only when both the bits are false. The bitwise EX-OR operation returns 1 if the input bits are different.

The following chart defines these bitwise binary operations
Variable
Decimal Equivalent
B3
B2
B1
B0
x
12
1
1
0
0
y
10
1
0
1
0
x&y
8
1
0
0
0
x|y
14
1
1
1
0
x^y
6
0
1
1
0

Bitwise NOT operation is the unary operator which flips the bits. It is also known as 1’s complement operator. This changes each 1 to 0 and 0 to 1.

Variable
Decimal Equivalent
B3
B2
B1
B0
x
12
1
1
0
0
~x
3
0
0
1
1

Bitwise SHIFT operators << and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be non negative.
 
When we shift left , the most significant bits are lost and the vacated least significant bits are zero. Similarly when we shift right least significant bits are lost and the most significant bits become zero.
 
Eg:
int x=5 // 0000 0000 0000 0101
y=x<<2; // x is shifted left two times and the result is stored in y.
so y become 20 //0000 0000 0001 0100
z=x>>2;// x is shifted right two times and the result is stored in z
so z become 1 // 0000 0000 0000 0001
 
Many programs can be written efficiently using bit wise operator, which otherwise require loops ,division etc..
Examples
1. Check whether the given number is even or odd
#include <stdio.h>
void main()
{
int n;
printf("enter the number..\n");
scanf("%i",&n);
if(n & 0x00000001)
printf("odd\n");
else
printf("even\n");
}
2.Check whether the given number is a power of 2.
#include <stdio.h>
int main()
{
int n;
printf("Enter the number..\n");
scanf("%d",&n);
if(n & (n-1)==0)
  printf("The number is Power of 2\n");
else
  printf("The number is not power of 2\n");
return 0;
}
3.Exchanging the values of two variables with out using a temporary variable
#include <stdio.h>
int main()
{
int a,b;
printf("Enter the a and b..\n");
scanf("%d %d",&a,&b);
a=a^b;
b=b^a;
a=a^b;
printf("The swapped values of a and b\n",a,b);
return 0;
}
4.16 bit Binary equivalent of an integer number
#include <stdio.h>
void main()
{
unsigned short int n,i;
unsigned short int mask=0x8000;
int t;
printf("enter the number..\n");
scanf("%hu",&n);
for(i=0;i<16;i++)
{
 t=n&mask;
 putchar(t==0?'0':'1');
 n=n<<1;
}
printf("\n");
}
5.printing the binary and also counting the number of 1's 
#include <stdio.h>
void main()
{
unsigned short int n;
int count=0;
unsigned short int mask=0x8000;
printf("enter the number..\n");
scanf("%hu",&n);
while(mask!=0)
{
if(n & mask)
   {printf("1");count++;}
else
    printf("0");
mask=mask>>1;
}
printf("\nNumber of ones=%d\n",count); 
}
6.Write an easy to read C program to process a set of n natural numbers and to find the largest even number and smallest odd number from the given set of numbers. The program should not use division and modulus operators.
#include <stdio.h>
void main()
{
int n,i,x,le=-1,so=9999;
printf("enter n..\n");
scanf("%i",&n);
printf("enter %d numbers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
if(x & 0x00000001)
   {if(x<so) so=x;}
else
   {if(x>le) le=x;}
}
printf("Largest even=%d smallest odd=%d\n",le,so);
}

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar