bitwise operation
Intro
This page will cover bit operations.
Of course, it is a wiringpi sub-document, but the content will never be limited to wiringpi.
Bitwise Operation
Bits operation
Comparison of values is key to bit-logic operations.
You checked about digital logic-gate in the previous document.
Then, you need to know the properties of and, or, and not for bitwise programming.
AND
And has the characteristic that if one of them has zero, result is zero.
To make it easier to understand, I’ll give you an example in two bits.
case 1
? & 1 = 0
what is “?“
case 2
? & 1 = 1
what is “?“
We know both answers.
Isn’t case 1 is 0 and case 2 is 1?
Some of you may have noticed here, but there is one characteristic.
The value of “?” is the same as the result value.
If you do the AND operation with 1, you protect it to original.
If then,
case 3
? & 0 = 0
what is “?“
Can you answer for sure between 0 and 1?
If you do the and operation with zero, you force it to zero.
Finally, If you want to change the position you want to zero, you can use AND operation.
target's bit2 and bit7 need to set 0.
target = 10110101;
target = target & 01111011;
Will you use 01111011 to write bit2 and bit7 as 0 when you actually code?
01111011 is not intuitive.
You can change it more intuitively using NOT here.
~(10000100)
It is more intuitive to mark only bit2 and bit7 as 1.
This is the power of NOT.
Modify the code like this.
target = 10110101;
target = target & ~(10000100);
target = target & ~(10000100); can be changed target &= ~(10000100);
To write zero where I want to be,
# if you want to change target's bit2 and bit7 as 0,
target &= ~(10000100);
OR
OR has the characteristic that if one of them has 1, result is 1.
To make it easier to understand, I’ll give you an example in two bits.
case 1
? | 0 = 0
what is “?“
case 2
? | 0 = 1
what is “?“
We know both answers.
Isn’t case 1 is 0 and case 2 is 1?
Some of you may have noticed here, but there is one characteristic.
The value of “?” is the same as the result value.
If you do the OR operation with zero, you protect it to original.
If then,
case 3
? | 1 = 1
what is “?“
Can you answer for sure between 0 and 1?
If you do the OR operation with 1, you force it to 1.
Finally, If you want to change the position you want to 1, you can use OR operation.
target's bit2 and bit7 need to set 1.
target = 10110101;
target = target | 10000100;
target = target | 10000100; can be changed target |= 10000100;
To write zero where I want to be,
# if you want to change target's bit2 and bit7 as 1,
target |= 10000100;
Shift operation
Shift operations change binary values by moving them.
There are « and » .
The direction of movement is the same as the shape
« moves binaries by filling the right-end with 0.
If the memory size is exceeded, the highest digit is discarded first.
For each space move, the value is value x 2 .
input: 0b00001010 << 2 deci: 10
result: 0b00101000 deci: 40 move 2 space, value: (value x2 x2)
» moves binaries by filling the left-end with 0.
If the memory size is exceeded, the highest digit is discarded first.
For each space move, the value is value / 2 .
input: 0b00010000 >> 3 deci: 16
result: 0b00000010 deci: 2 move 3 space, value: (value /2 /2 /2)