Nibbles

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

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 ?

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 “>>”  :-[

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

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 :wink:

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

:slight_smile:

1010 1110 &

0000 1111 =

0000 1110

0xAE & 0x0F = 0x0E

:slight_smile:

Nicely explained stryd_one :slight_smile:

Woohoo ! Thank you stryd_one  :-*

Thanks dude I was just wondering if I made an ounce of sense hahah

Stay tuned for me sharing my binary cheat sheet :wink:

Hallo stryd_one!

I’m not sure at the moment, but considering

http://www.sprut.de/electronic/pic/assemble/befehle.html#rrf

the high bit is filled with the carry flag on right shifts.

So you have to mask out the higher bits when you get te higher nipple, too.

unsigned char highernipple = (value >> 4)  & 0x0f;

unsigned char lowernipple = value & 0x0f;

Sorry! I thought that it would ignore that in C :\ Don’t know why though, heh!

These are low res screencaps of a word doc I made up, hope it might be handy :slight_smile:

Edit: If you want the word doc, PM me your email. Oh, and blame excel for any math errors heheh

Edit: Updated cheat-sheets

wow, that’s a nice thread :smiley:

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

That’s awesome. There’s gotta be something like that for winblows. I have a mission :wink:

Edit:

Results 1 - 100 of about 3,220,000 for programmers calculator

Yeh. I think that’s one for tomorrow :wink:

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 :wink: I really recommend you try it out, I was quite impressed.