Bits and bobs: Bits and Bytes
Hugh
Posts: 362
Hi,
This is probably a silly question, so feel free to provide a silly answer:
Q: Is there any easy way to convert a byte to bits, i.e., a byte with decimal value '38' to '00100110'?
Bitwise encode isn't quite it. I could write some code to do it, but an indigenous spin function would be much more preferable!
Thanks
Hugh
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Hugh - the thinking woman's Geoffrey Pyke.
This is probably a silly question, so feel free to provide a silly answer:
Q: Is there any easy way to convert a byte to bits, i.e., a byte with decimal value '38' to '00100110'?
Bitwise encode isn't quite it. I could write some code to do it, but an indigenous spin function would be much more preferable!
Thanks
Hugh
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Hugh - the thinking woman's Geoffrey Pyke.
Comments
If so then:
Otherwise you should realize that '38' and '00100110' are the exact same thing, unless they are strings.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
collection of eight binary bits, so the "conversion" is implicit.
Are you looking for the ASCII representation of the binary expansion, perhaps?
I meant that iff I have a byte with a value of '38', can I easily determine the value of Bit 3, Bit 7, Bit 'n', etc., is?
I think Kye's solution is sufficiently simple and zippy, but I didn't know whether there was a spin single command / function to do the same thing - it would appear not!
Thanks!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Hugh - the thinking woman's Geoffrey Pyke.
There is no command for what you want to do here.
((byte >> n) & 1) is the way to do this.
As in if I want bit 3 (from bit 7 to bit 0)·then I do.
((byte >> 3) & 1)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
This returns true if the bit is set, false if it is not.
[noparse][[/noparse]Edit] If you want to test for multiple bits on, you could do this:
So if you want to check if bits 0, 3, and 7 of a value are set you could do this:
Post Edited (JonnyMac) : 7/13/2009 7:19:17 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Hugh - the thinking woman's Geoffrey Pyke.
-Phil
You can just do:
OUTA:=$38
if OUTA[noparse][[/noparse]bit]
...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Composite NTSC sprite driver: Forum
NTSC & PAL driver templates: Forum
OnePinTVText driver: ObEx Forum