What Is Bit Logic and Why Do We Use It?

Bit logic is the procedure that includes comparing and calculating the binary numbers. Because computers or electronic circuits only understand ON and OFFs. Before getting to the bottom of bit logic (or bitwise operation), we have to know what is binary numbers.

bit logic image ceyrekasir.com


Binary Numbers

We use decimal numbers in general. It is the base-10 number system. But we can not use 10 in one digit. The largest number that we can use is 9 in a decimal number system (base-10 number system). A binary number is a base-2 number. It only includes 0 and 1. So, there will be no 2,3,4,5,6,7,8,9 on it.  For instance, if we represent a number that is larger than 1, we have to use more than one digits in the binary number system.

Here is a table of some binary numbers.

Numbers converted decimal to binary for bit logic operation
Decimal numbers to binary numbers that we will use them later for bit logic operations.

Decimal To Binary Converting

So, if you want to convert a decimal number to binary, I recommend you to start with dividing it by 2. You have a number that needs to be converted to binary, for instance, let’s suppose it is 50. First, divide it by 2, you have 25. The closest digit that larger than 25 is 32 which means 2^5. This is our biggest digit, now we have 100000 in the binary system. We have 18 to convert. The closest digit that smaller than 18 is 16 which means 2^4. It is 110000 as the total of 32 and 16. We have 2 and it is 2^1 which is 10 as binary. Total of all it is  110010 as binary. Here is the calculation.

Decimal to binary conversion
Decimal to binary conversion

Binary To Decimal Converting

Binary numbers have only 1 and 0. So, we are just adding 2^n, where n is the digit number. Here is the calculation.

Binary to decimal conversion
Binary to decimal conversion

As you can see above, an 8 digit binary number (also 8 bit or 1 byte) 11100101 is equal to 229 in the decimal number system.

If you want to see more details about the binary number system, I recommend you to see it from Maths is fun website. It is a detailed and really funny explanation.

Byte, Kilobyte, Megabyte

We are calling a digit of the binary number as a bit. Bit is the short version of binary digit. A byte is 8 digit of the binary number. So a byte is 8 bits. A kilobyte is 1024 byte and megabyte is 1024 kilobyte. Here is a short table about it.

Byte Bit
Byte 1 8
KiloByte 1024 8192
Megabyte 1048576 8388608

Bit Logic or Bitwise Operations

Bit logic or bitwise operations are comparing or calculating with binary numbers. Those operations are NOT, AND, OR, XOR, and bit shifts.

NOT Operation

In bitwise operations, NOT reverse each bit in binary numbers. Bits that are 1 become 0, also bits that are 0 become 1. This operation also complements a binary number.

For instance, we have 4-bit number that is 1001. After NOT operation it becomes 0110. What does complement mean? Well, a 4-bit binary can have a maximum decimal value of 15. We have 1001 that equals 9 in decimal. After NOT operation, it becomes 0110 that equals 6 in decimal. The sum of those two numbers become 15 in decimal.

Here is an 8-bit example for it.

8-bit binary not operant example
8-bit binary NOT operation example

AND Operation

As an operation, AND compares two or more bits. If all bits that compared are 1, it concludes 1. Otherwise, the result is 0. We can also name it as multiplication. If we will use it for binary numbers, they must be the equal length in digits. Each digit of a binary number compared with the exact same digit of another binary number.

AND operation: If both digits that compared are 1, result is 1. Or else 0.
AND operation: If both digits that compared are 1, the result is 1. Or else 0.

In the figure above, for the result 1, both bits that compared need to be 1. This type of comparison can be used where all conditions are needed to be met.

OR Operation

This operation result 0 if both bits that we compare are 0. If not, the result will be 1. So, it is enough to make the result 1, if one of the bits is 1.

OR operation: If both digits that compared are 0, result is 0. Or else 1.
OR operation: If both digits that compared are 0, the result is 0. Or else 1.

As we can see figure above, one of the bits needs to be 1 to make the result 1. This comparison is generally used where one proper condition is enough to complete the situation.

XOR Operation

XOR compares each bit and if both have the same value the result is 0. If not, then the result is 1. Here is an example.

XOR operation: If both digits that compared are equal, the result is 0. Or else 1.

This operation can be utilized to change the value of predetermined bits. It takes less time than other procedures to change the value of some bits or flags and save to a register address. We can use XOR to do the sum.

Bit Shifts

Bit shifting has many profitable usages. For example, we can use it for multiplying, dividing, packing data (bits), and saving data space in memory or storage. I will explain them but first I want to show you shifting types.

There are several kinds of bit shifting. Arithmetic shift contains right and left shifts that divide or multiply the number. A logical shift is doing the same thing for unsigned binaries. Another is the circular shift that rotates the bits in the binary numbers. Also, rotation of bits through carry bit is the same as the circular shift with the carry bit.

Arithmetic Shift (Logical Shift)

First of all, in those two kinds of shifts (arithmetic and logical shifts), they are identical, shift left or right. In arithmetic shift, right shifting does not change the most valuable bit (MSB), because the blank bit filled again with the same bit, as explained in the figure below. This is why there will be no difference in sign between the new value and the old value of the binary number. (Please check signed binary numbers that I explained below)

Arithmetic Shift
Arithmetic Shift

On the other hand, the left shift in both arithmetical and logical shifting is the same. Forwhy it fills 0 instead of the blank the least significant bit.

Logical Shift
Logical Shift

Circular Shift

As you can already know from below, we lose the most significant bits or the least significant bits (MSB or LSB). Despite, there will be any bit lose in the circular shift.

For instance, in right circular shift, the least significant bit becomes the most significant bit. Let’s assume we have 4-bit 0011 as a binary number. After the right circular shift, it becomes 1001. So there is no bit lose in such situation.

With exact 4 bit number, 0011. If we apply left circular shift, the most significant bit will be the least significant bit. The new binary number after this shift will be 0110.

Rotate Through The Carry Bit (Carry Flag)

This is the circular shift with the carry bit. The binary number does not lose any bits but swaps the most significant bit or the least significant bit with the carry bit.

At first, let’s say we have 1001-0, 4-bit binary with carry flag 0. Left rotate makes the most significant bit becomes carry bit and carry bit becomes the least significant bit. After left rotate, the new 4-bit binary will be 0010-1.

Secondly, 4-bit binary 1001-0 becomes 0100-1 after right rotate. The carry bit becomes the most significant bit and the least significant bit becomes carry flag.

Packing Data and Saving Data Space With Bit Shifting

Let’s assume we have 4 8-bit binary numbers. We need to send this number in one go. In one cycle, we are able to send 4 bytes at once. In one way or another, we have to link up that 4 numbers to make 4-byte.

Our numbers: a=166, b=200, c=235, d=119
Binary of our numbers: a=10100110, b=11001000, c=11101011, d=01110111
We need to link in those 4 numbers.

Step 1: So we need a 4 byte number that equals 0. Name it as x.
x = 00000000 00000000 00000000 00000000
Step 2: Use OR operation to place a into x. :This is the new x.
x = x OR a = 00000000 00000000 00000000 10100110
Step 3: Left bit shift x, 8 times.
x = x << 8(times) = 00000000 00000000 10100110 00000000
Step 4: This time use x OR b.
x = x OR b = 00000000 00000000 10100110 11001000
Step 5: Again, left shift 8 times.
x = x << 8(times) = 00000000 10100110 11001000 00000000
Step 6: Use OR, this once x OR c.
x = x OR c = 00000000 10100110 11001000 11101011
Step 7: For last time, left shift x 8 times.
x = x << 8 = 10100110 11001000 11101011 00000000
Step 8: Lastly, x OR d.
x = x Or d = 10100110 11001000 11101011 01110111

Now we had four 1-byte numbers packed in one 4-byte number. We can send it in one cycle. Otherwise, we will send them in 4 cycles and there would be a lot of zero in 4-byte numbers.

Signed Binary Number and Two’s Complement Number

There are 3 types of signed byte presentation. One of them is Sign-and-Magnitude (SM). Others are One’s Complement and Two’s Complement.

Sign-and-Magnitude (SM)

In this type of presentation, one bit is for the sign and other bits are for the magnitude of the number. If we have the 8-bit signed binary number, the first is the sign bit and other 7 bits are representing the magnitude.

Sign-and-Magnitude Representation
Sign-and-magnitude Representation

This method has a minus, when we represent the sign with this method, we lose one bit and it decreases its magnitude range.

One’s Complement

This method inverts each bit of positive number to show its negative representation. For instance, -7 is the inversion of 7. 7 is 0111 and -7 is 1000 in one’s complement. Similarly, 5 is 0101 and -5 is 1010.

Two’s Complement

Similar to one’s complement, it inverses positive number but adds 1 after negation. We saw examples above, differently, -7 is 1001 and -5 is 1011 in two’s complement. This representation method is the most preferred in computers.

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.