Let us consider two 8 bit numbers a and b where a = 36 and b = 6.
2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 | |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
36 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
6 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
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 |
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
DescriptionBasically, 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
DescriptionThe 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 |
#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
DescriptionThe 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 |
#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
DescriptionThe 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 |
#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!