bill Posted June 16, 2006 Report Share Posted June 16, 2006 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 0xfI dont know very well how to explain, but i'm sure you do understand ;DPlease help me !Thanks Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 16, 2006 Report Share Posted June 16, 2006 You mean like:0x4F becomes0x04 and0x0F ?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 becomes0x40 and0x0F ? Quote Link to comment Share on other sites More sharing options...
bill Posted June 16, 2006 Author Report Share Posted June 16, 2006 Yes, what i want to get is this 0x4F becomes 0x04 and 0x0Fplease 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 ">>" :-[ Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 16, 2006 Report Share Posted June 16, 2006 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 Low0100 1111First 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 >> 4That 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 4Now 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 = 0x0FAs 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 11101010 1110 >> 4 = 000010100xAE >> 4 = 0x0A :)1010 1110 &0000 1111 = 0000 11100xAE & 0x0F = 0x0E:) Quote Link to comment Share on other sites More sharing options...
th0mas Posted June 16, 2006 Report Share Posted June 16, 2006 Nicely explained stryd_one :) Quote Link to comment Share on other sites More sharing options...
bill Posted June 16, 2006 Author Report Share Posted June 16, 2006 Woohoo ! Thank you stryd_one :-* Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 16, 2006 Report Share Posted June 16, 2006 Thanks dude I was just wondering if I made an ounce of sense hahahStay tuned for me sharing my binary cheat sheet ;) Quote Link to comment Share on other sites More sharing options...
Thomas Posted June 16, 2006 Report Share Posted June 16, 2006 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; Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 16, 2006 Report Share Posted June 16, 2006 Sorry! I thought that it would ignore that in C :\ Don't know why though, heh! Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 16, 2006 Report Share Posted June 16, 2006 These are low res screencaps of a word doc I made up, hope it might be handy :)Edit: If you want the word doc, PM me your email. Oh, and blame excel for any math errors hehehEdit: Updated cheat-sheets Quote Link to comment Share on other sites More sharing options...
audiocommander Posted June 16, 2006 Report Share Posted June 16, 2006 wow, that's a nice thread :DI 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.comThere'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 Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 16, 2006 Report Share Posted June 16, 2006 That's awesome. There's gotta be something like that for winblows. I have a mission ;)Edit:Results 1 - 100 of about 3,220,000 for programmers calculatorYeh. I think that's one for tomorrow ;) Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 17, 2006 Report Share Posted June 17, 2006 For Windows or Linux I came across this much simpler calc:http://www.muquit.com/muquit/software/mbasecalc/mbasecalc.html#downloadBut 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.