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 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;
}
2.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;
}
3.16 bit Binary equivalent of an integer number
#include <stdio.h>
void main()
{
unsigned int n,i;
unsigned int mask=0x8000;
int t;
printf("enter the number..\n");
scanf("%u",&n);
 for(i=0;i<16;i++)
 {
 t=n&mask;
 putchar(t==0?'0':'1');
 n=n<<1;
}

}

Comments

Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Strings in C

Strings in C Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string. charstr_name[size]; In the above syntax str_name is any name given to the string variable and size is used define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character (‘\0’) used to indicate termination of string which differs strings from normal character arrays. Initializing a String: A string can be initialized in different ways. We will explain this with the help of an example. Below is an example to declare a string with name as str and initialize it with “cek”. 1. charstr[] = "cek"; 2. charstr[50] = "cek"; 3. charstr[] = {'c','…