30 March 2016

Bitwise operators in C with detail analysis and examples

Bitwise operators

Why bit operators is required to learn

Because when we want to do embedded programming .it is necessary to know bit operation because we want to interact directly to hardware.
In bit operation, 6 terms are used.
Left shift, right shift,AND,OR,negation(~) and exor.


Left Shift
It is used for shifting the binary digit to the left. We can say that we simply multiply the digit with 2.It is represented by 24«1 .
Use: It is used to set the particular bit in it to 1.
Note:
4bits together form a nibble.
16 bit form a word and 32 bit form a double bit.
Bit operation can't perform on double and float
For printing value in hexadecimal use %x and for capital latter %X, unsigned value i.e address %u.

Right shift
It means divide the value by 2 and ignore reminder. If the value is not division by 2 it takes like 13/2=6. It is represented by » .no of bits will move depend on right side digit.
Bitwise and (&): when both operand value will one then result will one.
Use: It is used to check whether a particular bit of an operand is on or off
To turn off a particular bit.

OR( |):  It gives result 1 when either one has 1.
It is used to put on a particular bit in a number.

EXOR -when both values is same then the output will be 0 otherwise 1. It is the  most used bit operator. Like adding two value without using + operator. Checking two integer value is same or not etc.

A negative number is represented by one's complement. But it gives so much undesired result. Therefore, 2's complement mostly used for a negative number in the computer.
Like if we want to represent -1 then binary value of 1 is 00000001. Negation of 1 is 11111110 and after adding 1 it will become 11111111. 2's value is equal to 2^8-x. I.e 255 which binary value is 11111111.more visit this nice elaborated article on it.
http://www.programminglogic.com/how-computers-represent-negative-binary-numbers/
If you have something different knowledge regarding this. Please must comment and acknowledge others.

No comments:

Post a Comment

Ads Inside Post