The magic of bits "Bitwise"
Introduction to bitwise - date: 24/11/2011
Last updated
Introduction to bitwise - date: 24/11/2011
Last updated
Before the long tale to the course of magic bits, let's go for a little walk in the world of C language. In C, the bitwise operators allow you to manipulate individual bits in a value. This can be useful for certain low-level operations, such as setting or checking the value of a specific bit in a flag. For example, you could use a bitwise operator to set the fourth bit in a value to 1 without affecting the other bits. This can be useful for optimization and make your code more efficient by allowing you to perform certain operations using bit manipulation instead of more complex methods. Additionally, the use of bitwise operators can make your code more compact and easier to read because you can perform multiple operations using a single operator instead of using multiple lines of code. Variable of type int has 4 bytes, so 32bits.
If we do it:
"int var = 3;"
We can see this schema:
4bytes = 32bits
Looking at this context, each octet is a byte. Half an octet can be called a nibble.
“…00000000 00000000 00000011”
Each place of a binary number counts as one bit. The bit is nothing but a mnemonic for “Binary digiT”. So to know the number of bytes, we use the “sizeof(var)” operator, which returns the value in bytes of the variable. If we want to know the binary value of a number, we divide by 2, and as long as the remainder of the division is different from “1”, we divide the end. We take the leftovers in reverse to be the final result, for example, number 12:
Going on, there's another better way to do it, but no it's time to address this now.
Introduction to "Bitwise" When we say bitwise, it is a mere reference to work to manipulate bits to get specific results. Staff who work with microcontrollers, AVR and PIC often have to do such manipulation. When we want bitwise performance, we can also help, although the compiler already optimizes many tasks. Another use is in image processing and manipulation. OpenCV even forces you to use it whenever there is no ready-made solution.
So looking at C language, the left shift operator "<<" shifts the bits of a value to the left by a specified number of positions. This has the effect of multiplying the value by a power of 2. For example, if you shift the value 4 left by 1 position, the result would be 8 because 4 * 2 = 8.
Looking to another hand, the right shift operator ">>" does the opposite, shifting the bits of a value to the right by a specified number of positions. This has the effect of dividing the value by a power of 2. For example, if you shift the value 8 right by 1 position, the result would be 4 because 8 / 2 = 4.
One motivation for using the left shift and right shift operators is to perform multiplication and division by powers of 2 quickly and efficiently. This can be useful for optimization, especially in time-critical operations where every clock cycle counts. Another motivation is to use these operators to manipulate individual bits in a value, which can be helpful for various purposes, such as setting or checking the value of specific bits in a flag.
Shifting is nothing more than changing the bit from its original position. To get a specific result, we call this technique bit shift a mnemonic of the assembly “shl”(shifting left) and shr(shifting right). Let's look at an example of shifting to the left:
The result is “6”, which can give an illusion of product arithmetic or addition by itself, but it was the result of displacement, formed correctly according to the K&R math textbook to explain our expression. An example would be “2*3¹”.
Now look that following a shift to the right:
int
var = 100;
So you can see 1100100
The result gives us “25” the mathematical form for this is the following “(100/2)²”.
You tell me 25 thousand. Where are the zeros? As I said, remove the last two digits.
OR
Let's go to the “|” operator with the “OR” mnemonic in assembly. Let's go to understand its impact.
AND
Now the “&” mnemonic with “AND” in assembly.
The "~" is mnemonic with "NOT"; that is, it is a negation, making an inverse effect of its loaded value, where one is, it becomes zero and vice versa. So the result would be -27, Like, but why not 5? Remember that I said an "int" is 4 bytes equals 32bits, so look at the following:
0000 0000 0001 1010
1111 1111 1110 0101
I did it in a nibble, so I don't have to write too much.
XOR
The “^” is a mnemonic for XOR.
So look at that table in the following:
Let's use bitwise to get "performance" let's go to get
some bitwise code snips.
So remember our simple code to convert decimal to binary? Let's make one using bitwise 🙂
The square root calc, following bitwise path:
This function cannot be compared to APIs such as GMP and OpenSSL even because it is a simple function, much less “math.h” it was more to illustrate.
If it is a pointer, why not?
So this second example is a simple benchmark following CPU cycles to compare arithmetic division using bitwise and common resources.
This subject is huge. I'll stop here for the end. I suggest you read it to delve deeper into the subject by bitwise, the following book “hacker’s delight".
Thank you for reading, cheers!