Bit Manipulation


Author Message
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 11:28pm 03 Mar 2012      

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: Australia
Posts: 3368
Posted: 12:11am 04 Mar 2012      

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 Zealand
Posts: 711
Posted: 12:43am 04 Mar 2012      

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: Australia
Posts: 164
Posted: 03:16am 04 Mar 2012      

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: Australia
Posts: 3368
Posted: 09:00am 04 Mar 2012      

  crackerjack said  I hope to update them to use Sub's but may wait for 3.2

Yes, definable functions would make that a dream.

Geoff
Geoff Graham - http://geoffg.net

paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 10:23am 04 Mar 2012      

  Geoffg said  
  crackerjack said  I hope to update them to use Sub's but may wait for 3.2

Yes, definable functions would make that a dream.

Geoff
They seem like a very good candidate for the MM library. Greg