Monday, October 17, 2011

Bitwise Operators in ANSI C

The basic bitwise operations are NOT, AND, OR and XOR.

Let us consider two 8 bit numbers a and b where a = 36 and b = 6.


2^72^62^52^42^32^22^12^0

1286432168421
3600100100
600000110

The best way for working with bits and bytes is by using the stdint.h ANSI library. This library helps you by making your code more portable (some compilers have 16 bit integers and 32 bit long integers, while others have 32 bit integers and 64 bit long integers)
Binary decomposition example
stdint.h provides 8, 16, 32 and 64 bit compiler-independent primitive data types both signed and unsigned.

In the examples below, I will use unsigned 8 bit integers, which shall translate to the uint8_t type.

To include the library when using C:
#include<stdint.h>
1.NOT operation
Description
Basically, negating a number means that every 0 becomes and every 1 becomes 0.

a 0 0 1 0 0 1 0 0 36
~a 1 1 0 1 1 0 1 1 219
b 0 0 0 0 0 1 1 0 6
~b 1 1 1 1 1 0 0 1 249

Syntax
#include<stdio.h>
#include<stdint.h>

int main(void)
{
   uint8_t a = 36;
   uint8_t b = 6;
   uint8_t nota = ~a;
   uint8_t notb = ~b;
   printf("~a %u\n",nota);
   printf("~b %u\n",notb);
   return 0;
}
/*Output
~a 219
~b 249
*/
2.AND operation
Description
The bitwise AND operation compares the bits on every position and if both bits are 1, the result bit will be 1. Otherwise, the result bit will be 0.
a 0 0 1 0 0 1 0 0 36
b 0 0 0 0 0 1 1 0 6
a AND b 0 0 0 0 0 1 0 0 4
Syntax
#include<stdio.h>
#include<stdint.h>

int main(void)
{
   uint8_t a = 36;
   uint8_t b = 6;
   uint8_t aandb = a&b;
   printf("a&b %u\n",aandb);
   return 0;
}
/*Output
a&b 4
*/ 
3.OR operation
Description
The bitwise OR operation compares the bits on every position and if one of the bits is 1, the result will be 1. Otherwise (if both bits are 0), the result will be 0.
a 0 0 1 0 0 1 0 0 36
b 0 0 0 0 0 1 1 0 6
a OR b 0 0 1 0 0 1 1 0 38
Syntax
#include<stdio.h>
#include<stdint.h>

int main(void)
{
   uint8_t a = 36;
   uint8_t b = 6;
   uint8_t aorb = a|b;
   printf("a|b %u",aorb);
   return 0;
}
/*Output
a|b 38
*/
4.XOR operation
Description
The bitwise XOR operation compares the bits on every position and if one of the bits is 1 and the other is 0 the result will be 1. Otherwise (if both bits are 1 or 0) the result will be 0.
a 0 0 1 0 0 1 0 0 36
b 0 0 0 0 0 1 1 0 6
a XOR b 0 0 1 0 0 0 1 0 34
Syntax
#include<stdio.h>
#include<stdint.h>

int main(void)
{
   uint8_t a = 36;
   uint8_t b = 6;
   uint8_t axorb = a^b;
   printf("a^b %u",axorb);
   return 0;
}
/*Output
a^b 34
*/
Below you can see the logic table for the AND, OR and XOR operations:
a b AND OR XOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0


Question: In which context do you use bitwise operations?

References:
http://en.wikipedia.org/wiki/Bitwise_operations_in_C

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger