Conversion of Byte to 8 Bits
John Ritz
Posts: 14
What's the easiest and quickest way to convert from a byte variable to its 8 bits in a BS2?
For example, I have the following:
MyByte VAR BYTE
MyBit0 VAR BIT
MyBit1 VAR BIT
MyBit2 VAR BIT
MyBit3 VAR BIT
MyBit4 VAR BIT
MyBit5 VAR BIT
MyBit6 VAR BIT
MyBit7 VAR BIT
I want to be able to set MyBit0 through MyBit7 to equal bits 0 - 7 respectively of MyByte.
Thanks!
For example, I have the following:
MyByte VAR BYTE
MyBit0 VAR BIT
MyBit1 VAR BIT
MyBit2 VAR BIT
MyBit3 VAR BIT
MyBit4 VAR BIT
MyBit5 VAR BIT
MyBit6 VAR BIT
MyBit7 VAR BIT
I want to be able to set MyBit0 through MyBit7 to equal bits 0 - 7 respectively of MyByte.
Thanks!
Comments
thing· var· byte
thing0 var thing.bit0
thing1 var thing.bit1
thing2 var thing.bit2
and so
Now you have 8 variables corresponding to the 8 bits of thing byte.· You can now assign values to those bit variables.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Do you have a Stamp Tester?
http://hometown.aol.com/newzed/index.html
·
myByte = 0
myByte.BIT3 = 1 ' myByte now holds 8
You can also index through the bits of a variable.
FOR idx = 0 TO 7
· myByte.LOWBIT(idx) = 1
NEXT
(Silly example, I know, I simply wanted to demonstrate the use of the LOWBIT() modifier)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
So if I want to get bits 0 - 7 of a byte variable I can just do this:
Just to confirm, in a byte variable, you would always use LOWBIT, but in a word variable, you could use LOWBIT and HIGHBIT, correct?
I could also do this then per Newzed:
If I was concerned with the code overhead time of LOWBIT (which I am assuming is higher than .bitx)
If I am correct here, this should suit my program nicely!
Thanks...John
Yes, using the LOWBIT modifier adds some overhead, but it also adds flexibility that you don't get with straight aliasing.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA