c bitwise operators

4
C Bitwise Operators Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long, here we will see the basics of bitwise operators, and some useful tips for manipulating the bits to achieve a task. Bitwise operators operate on individual bits of integer (int and long) values. This is useful for writing low- level hardware or OS code where the ordinary abstractions of numbers, characters, pointers, and so on, are insufficient. Bit manipulation code tends to be less "portable". Code is "portable" if without any programmer intervention it compiles and runs correctly on different types of computers. The bit wise operations are commonly used with unsigned types. These operators perform bit wise logical operations on values. Both operands must be of the same type and width: the resultant value will also be this type and width. A bitwise operator works on each bit of data. Bitwise operators are used in bit level programming the Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13 then: Operat or Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 | Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right A << 2 will give 240 which is 1111 0000

Upload: suneel-dogra

Post on 12-Jan-2015

161 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C bitwise operators

C Bitwise Operators

Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long, here we will see the basics of bitwise operators, and some useful tips for manipulating the bits to achieve a task. Bitwise operators operate on individual bits of integer (int and long) values. This is useful for writing low-level hardware or OS code where the ordinary abstractions of numbers, characters, pointers, and so on, are insufficient. Bit manipulation code tends to be less "portable". Code is "portable" if without any programmer intervention it compiles and runs correctly on different types of computers. The bit wise operations are commonly used with unsigned types.  These operators perform bit wise logical operations on values. Both operands must be of the same type and width: the resultant value will also be this type and width. A bitwise operator works on each bit of data. Bitwise operators are used in bit level programming the Bitwise operators supported by C language are listed in the following table.

Assume variable A holds 60 and variable B holds 13 then:

Operator Description Example

&Binary AND Operator copies a bit to the result if it exists in both operands.

(A & B) will give 12 which is 0000 1100

|Binary OR Operator copies a bit if it exists in either operand.

(A | B) will give 61 which is 0011 1101

^Binary XOR Operator copies the bit if it is set in one operand but not both.

(A ^ B) will give 49 which is 0011 0001

~Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.

(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.

<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.

A << 2 will give 240 which is 1111 0000

>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

A >> 2 will give 15 which is 0000 1111

Bitwise OR – |

Bitwise OR operator | takes 2 bit patterns, and perform OR operations on each pair of corresponding bits. The following example will explain it.

1010 1100 --------OR 1110 --------

The Bitwise OR, will take pair of bits from each position, and if any one of the bit is 1, the result on that position will be 1.

Page 2: C bitwise operators

Bitwise AND – &

Bitwise AND operator &, takes 2 bit patterns, and perform AND operations with it.

1010 1100 -------AND 1000 -------

The Bitwise AND will take pair of bits from each position, and if only both the bit is 1, the result on that position will be 1

One’s Complement operator – ~

One’s complement operator (Bitwise NOT) is used to convert each “1-bit to 0-bit” and “0-bit to 1-bit”, in the given binary pattern. It is a unary operator i.e. it takes only one operand.

1001NOT ------- 0110 -------

Bitwise XOR – ^

Bitwise XOR ^, takes 2 bit patterns and perform XOR operation with it.

0101 0110 ------XOR 0011 ------

The Bitwise XOR will take pair of bits from each position, and if both the bits are different, the result on that position will be 1. If both bits are same, then the result on that position is 0.

Bit a Bit b a & b a | b a ^ b ~a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 #include<stdio.h>#include<conio.h>void main(){int a=5,b=3;printf("%d\n",a|b);printf("%d\n",a&b);printf("%d\n",a^b);getch();}

Page 3: C bitwise operators

Right Shift a = 0000 0011 = 3(a<<=1) = 00000110 = 6(a<<=2) = 00011000 = 24(a<<=3) = 11000000 = 192

Left Shift a=11000000     =192(a>>=1) = 01100000 = 96(a>>=2) = 00011000 = 24(a>>=3) = 0000 0011 = 3

a = 12b = 10---------------------------------a in Binary : 0000 0000 0000 1100b in Binary : 0000 0000 0000 1010---------------------------------a | b : 0000 0000 0000 1110a & b : 0000 0000 0000 1000a ^ b : 0000 0000 0000 0110

a | b : 0000 0000 0000 1110 = 14a & b : 0000 0000 0000 1000 = 8a ^ b : 0000 0000 0000 0110 = 6