Reading and Writing Bits in Bytes


This is a very old article written by me (around 2001, I guess) and it is just here for record.

Welcome, this is a tutorial concerning digital arithmetic of binary numbers. It will show you how to read and write bits from bytes, words and potentially any other data type.

First, let me tell you something about data types. There are many types of data types. But, here we will be dealing only the sizes of those data types. The basic data type is byte which is 8 bits long, then word or Integer which is 16 bits long, then comes dword(double-word) or Long Integer which is 32 bits long. These are standard QB data types. The others are extended integer which is 80 bits long.

The arrangement of bits in any data type is as follows. The LSB(least significant bit) starts from the right numbered 0 and goes to the length of the data type. Consider this bit arrangement in a byte.
7 6 5 4 3 2 1 0
This is the general arrangement. Any data type can be written this way. For example, a Long Integer will be written this way.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Now, the real fun. Checking a bit for its state. That is to check whether a bit is set(1) or reset(0). First, see this code.

temp = 2 ^ bitposition
a = ((var AND temp) = temp)

Here, bitposition is the number of bit to check and var is the variable in which it has to check.

Let me explain about that. First, a mask is created in which the bit position which you want to test is set and all others are reset. For example, you want to create a mask for a byte for bit position 2, then you have to created a mask like this.

7 6 5 4 3 2 1 0
0 0 0 0 0 1 0 0

The numbers above are bit positions. Notice that bit position 2 is set as we have to check for it. But how do we create this mask? Well, here’s it. The simplest way(in QB) to create such a mask is to raise 2 to the power of bit position. There is a better(and faster) way to do the same thing in Assembly, but it is beyond the scope of this tutorial.

OK, now you have the mask. Now, all you have to do is AND it with the variable var and test if the result is mask. When, the mask is ANDed with the variable, all other bits except the bit position you wanted to test become 0 as a result of AND. If you originally had a 1 in the bit position of the variable var, then AND will return 1 in the result. On the other hand, if there was a 0 in the var at bit position, then the result will also have 0. The theory behind this is to just keep the bit you want to check for the same; and reset all others.

Now, for the final part. Once you get the result of ANDing, then it has only two possibilities. One possibility is that if the bit you checked for was zero, you get zero as the final result. Or, if there was a 1 in the bit, then you get back your mask! So, all you have to do is get the result and compare it with mask. If it is true, then bit position was set, otherwise it was 0.

Now, for writing bits into bytes. It is basically the same as above but, instead of AND, you use OR.

The code again.

var = var OR (2 ^ bitposition) 'For setting a bit only
var = var AND (NOT (2 ^ bitposition)) 'For resetting a bit only

Here, var is the variable you want to modify and bitposition is the bit you want to set or reset.

First, I will tell you about setting a bit. To set a bit, you need to make a mask in which the bit in bit position is 1 and all others are 0. This is similiar to mask developed earlier.

7 6 5 4 3 2 1 0
0 0 0 0 0 1 0 0

As, you see, bit in the position 2 is set as we want to set that here. Now, simply OR this value to variable var. The OR operation will set the bit in bit position regardless of it’s previous value. All other bits will remain unchanged.

Now about, resetting bit. This is done with AND operation. We take the same mask as earlier but we inverse all the bits in a NOT operation.

7 6 5 4 3 2 1 0
1 1 1 1 1 0 1 1

As you see here that the bit which we want reset is 0 in the mask. Now, we AND this value to var. Here, the bit position you want to reset is reset to 0 and again all other bits remain unchanged.

So, this completes this tutorial on reading and writing bits in bytes and other data types. There is actually much more to this like Reading and writing multiple bits in a byte or any other data type. But, if you want to know them, contact me.