Jump to content

Nibbles


bill
 Share

Recommended Posts

I would like to work with 4bit values, and store 2 of them as a byte.

For example, i want to store the value 0x4f, then read separated values 0x4 and 0xf

I dont know very well how to explain, but i'm sure you do understand  ;D

Please help me !

Thanks

Link to comment
Share on other sites

You mean like:

0x4F becomes

0x04 and

0x0F ?

Because SDCC requires 8-bit bytes and no bitfields you'll have to read the whole byte, and then grab the bits you need by masking (AND) them and shifting (>>) them.

Or do you mean

0x4F becomes

0x40 and

0x0F ?

Link to comment
Share on other sites

Yes, what i want to get is this

0x4F becomes 0x04 and 0x0F

please can you explain me how to "grab the bits i need by masking (AND) them and shifting (>>) them."

I hae no clue how to use "AND" and ">>"  :-[

Link to comment
Share on other sites

Sorry bill, you are pretty advanced and I assumed.... my bad :-[ Please take it as a compliment ;)

So we take 0x4F. It's easier to represent in binary, so....

High Low

0100 1111

First we do the high nibble...

For this, we want to take the '4' and slide it over to the right. For this we use a bitwise right shift, which is represented as >> . You specify how many bits you want to shift like this:

Thing = doodad >> 4

That shifts the whole byte to the right by four bits. The 'gap' on the left side is filled with 0's. The low nibble is shifted right onto the ground plane and into oblivion ;)

so 01001111b >> 4  

= 0000 0100

= 0x 0 4

Now the low nibble. For this, we just want to get rid of the high nibble and leave the low one as it is. For this we do a bitwise AND. An AND operation takes two binary inputs, and if both the first AND the second one are a 1, it returns a 1, otherwise, it returns a 0. So to ensure that each of the bits of the high nibble return 0, you AND those bits with a 0. This might help:

0100 1111   THIS 1

  AND

0000 1111   THIS 1?

0000 1111 = 0x0F

As luck would have it, 0x4F does not demonstrate as well as I would like, so here's the two procedures for 0xAE:

0x A E

1010 1110

1010 1110 >> 4 = 00001010

0xAE >> 4 = 0x0A

:)

1010 1110 &

0000 1111 =

0000 1110

0xAE & 0x0F = 0x0E

:)

Link to comment
Share on other sites

wow, that's a nice thread :D

I don't know about windows, but on the mac there's a calculator with a programmer's mode. That's pretty nice to understand >> and << and AND... when you see what's happening with the bits.

I can also recommend a bash tool called "ch" from http://www.softintegration.com

There's a free version available! It's a C console interpreter, that's very handy to test some values and settings!

(and of course also available for DOS!)

Cheers,

Michael

86_calculator_jpg4159e2376bd787d521706b7

88_ch_pngaef72a8e0ad7fcb4b98d43dc5786a08

Link to comment
Share on other sites

For Windows or Linux I came across this much simpler calc:

http://www.muquit.com/muquit/software/mbasecalc/mbasecalc.html#download

But there may be better stuff for linux...

For Windows I found this awesome calculator, pmaCalc : http://www.pmasoft.net/download.htm It's free for private use and does pretty much everything I could think of and then some. Oh, and there's a German version, which could be handy for a few people around here ;) I really recommend you try it out, I was quite impressed.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...