Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Bit Manipulation
Author | Message | ||||
jman Guru Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Hi I have a 16 bit register that requires the data to be sent as 2 8 bit bytes. The value that needs to be sent is 8 bits and needs to be shifted to the right by 6 For example &B11000011 needs to be represented as HighByte = &B00110000 LowByte = &B11000000 To get the correct 16 bit number I can times the number by 64 so &B11000011 x &B1000000 = &B0011000011000000 So now we it shifted to the left by 6 places I Can get the HighByte by &B11000011 / &B100 = &B00110000 Now to get the LowByte = STUCK Any Help would be greatly appreciated Regards John |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3194 |
Hi John, The AND operator actually does a bitwise and operation. So, to get the low byte you can do: &B0011000011000000 AND &B11111111 and you should get &B11000000 as the result. If you are shifting right you should use the integer divide operator (\) ie, a back slash. This will discard any fractional value as a result of the divide. So, to get the high byte you can do: &B0011000011000000 \ 256 and you should get &B00110000 as the result. Geoff Geoff Graham - http://geoffg.net |
||||
jman Guru Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Hi Geoff Thanks for the explanation. Works perfectly I knew the clever folk would have a simple way to it :) Thanks John |
||||
crackerjack Senior Member Joined: 11/07/2011 Location: AustraliaPosts: 164 |
Jman, I posted some bitwise routines here: http://www.thebackshed.com/forum/forum_posts.asp?TID=4218 I hope to update them to use Sub's but may wait for 3.2 Cheers. |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3194 |
Yes, definable functions would make that a dream. Geoff Geoff Graham - http://geoffg.net |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
They seem like a very good candidate for the MM library. Greg |
||||
Print this page |