Reading and Writing Nibbles in Bytes


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

This is another tutorial in the category of Modifying bytes on bits and nibbles level. This one deals with nibbles. If you want to know about reading bits, read the tutorial which I have written earlier here.

So, we start again with another project and that is about Reading Nibbles from bytes. So, lets first talk about nibbles arrangement in bytes. As you must be knowing, there are 2 nibbles present in bytes. This is the arrangement.

[highest significant nibble] [least signinificant nibble2]

So, as you can see from above, there are 2 nibbles in a byte. There are 8 bits in a byte and so there are 4 bits in a nibble. So, nibble can effectively hold numbers from 0 to 15. You also need to know about hexadecimal numbers. Numbers 0 to 15 are represented by 0h to Fh. So, 2 nibbles in a byte are represented as 00h – FFh. For example, in the byte A0h, the highest significant nibble is A(10) and least significant nibble is 0.

So, lets start with modifying each nibble. This is actually very simple. You need to keep in the mind about the position of the nibbles in the byte. The two nibbles will now be called HSN(Highest significant nibble) and LSN(Least significant nibble). See the arrangement of the nibbles and read the above paragraph again if you are not sure about what I am talking.

You also need to know about the operators of AND and OR. With this information, you can start with the tutorial.

Lets first speak about reading nibbles. This is fairly simple. From the arrangement you see above, you can derive the following code. But first, let me tell you something. Let us define some codes for HSN and LSN. We will make LSN, 0 and HSN as 1.

nibble% = data%
IF nibpos% = 1 THEN nibble% = nibble% \ 16
nibble% = data% AND &HF

Let us understand the above code. First the byte(data%) is saved in nibble% so that now only nibble% has to be accessed. The variable nibpos% holds either 1 or 0 telling whether you want HSN(1) or LSN(0). Next, if you want the HSN, then you have to bring the HSN to LSN position. This can be done by dividing it by 16. This moves the HSN to LSN position. If you wanted LSN, then the nibble will not be divided as the LSN is already in the LSN position. As said earlier, I have defined HSN as 1 and LSN as 0. You can use constants for that purpose too. I will give you another code to make that clear.

CONST HSN = 1
CONST LSN = 0

nibble% = data%
IF nibpos% = HSN THEN nibble% = nibble% \ 16
nibble% = data% AND &HF

As you have seen, the Constant LSN is not used at all and is therefore of no use. I have just put it for clarity purposes. You don’t need to include it. And now for the last line of the code. In certain cases, the variable nibble% will not hold the final data. It will hold the nibble you wanted in the position of LSN. But, HSN may also be filled with some other value. So, here HSN is cleared and only LSN is permitted to stay using the AND operand. As a result, of the following code, you get the nibble in the variable nibble% from the initial byte data%.

OK, Now lets move on to writing nibbles into bytes. Again, I define HSN as 1 and LSN as 0.

CONST HSN = 1

IF nibpos% = HSN THEN
  nibble% = nibble% * 16
  data% = data% AND &HF
ELSE
  nibble% = nibble% AND &HF
  data% = data% AND &HF0
END IF
data% = data% AND nibble%

So, let me explain the code again. First, nibpos% tells the code whether to write to HSN or LSN. If it was HSN(1), the code first shifts the LSN of nibble% to HSN by multiplying the nibble% with 16. The HSN of data% is also cleared so that nibble% can be inserted through AND. Now, lets consider the case of writing to LSN. In this case, the HSN of nibble% and LSN of data% are cleared so that the nibble can be inserted through AND. Now, initial checks are completed and the final write is performed through the AND operator in the last statement.

If it was HSN which was written, then nibble in nibble% which was moved to HSN will be ANDed with data% which only has LSN and merging both. And in the case of LSN, there is no need of shifting data in nibble% as it is already in the LSN position and the HSN of data% is also cleared to facilitate AND operation. Now, as all data is intialized, AND is performed and they are merged.