PDA

View Full Version : Bits and bobs: Bits and Bytes



Hugh
07-14-2009, 02:10 AM
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.

Kye
07-14-2009, 02:19 AM
Um, your talking about strings right?

If so then:



VAR


byte binaryCharacters[33]


PUB numberToBinary(number, length) '' 5 Stack Longs

'' ┌───────────────────────────────────────────────── ────────────────────────────────────────────────── ───────────────────────┐
'' │ Converts an integer number to the binary string of that number padded with zeros. │
'' │ │
'' │ Returns a pointer to the converted string. │
'' │ │
'' │ Number - A 32 bit signed integer number to be converted to a string. │
'' │ Length - The length of the converted string, negative numbers need a length of 32 for sign extension. │
'' └───────────────────────────────────────────────── ────────────────────────────────────────────────── ───────────────────────┘

repeat result from 31 to 0

binaryCharacters[result] := ((number & $1) + "0")
number >>= 1

return @binaryCharacters[(32 - ((length <# 32) #> 0))]

Otherwise you should realize that '38' and '00100110' are the exact same thing, unless they are strings.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

rokicki
07-14-2009, 02:19 AM
Can you explain more fully what you are trying to do? A "byte" is just a convenient word we use for a
collection of eight binary bits, so the "conversion" is implicit.

Are you looking for the ASCII representation of the binary expansion, perhaps?

Hugh
07-14-2009, 02:29 AM
Sorry.

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.

Kye
07-14-2009, 02:43 AM
Oh, well then my solution will not be useful to you.

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,

JonnyMac
07-14-2009, 03:05 AM
You could code Kye's suggestion into a generalized method:



pub bittest(value, bit)
return (value & (1 << bit)) > 0


This returns true if the bit is set, false if it is not.


[Edit] If you want to test for multiple bits on, you could do this:



masktest(value, mask)
return (value & mask) == mask


So if you want to check if bits 0, 3, and 7 of a value are set you could do this:



status := masktest(somevar, %10001001)

Post Edited (JonnyMac) : 7/13/2009 7:19:17 PM GMT

Hugh
07-14-2009, 03:10 AM
More than good enough for me - Mr Lazy!

http://forums.parallax.com/images/smilies/burger.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Hugh - the thinking woman's Geoffrey Pyke.

Phil Pilgrim (PhiPi)
07-14-2009, 03:32 AM
Here are a couple other ways: one which uses an array, for when the bit number is a variable; the other, using constants, for when the bit number is a constant.




CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

bit0 = |< 0
bit1 = |< 1
'...
bit30 = |< 30
bit31 = |< 31

VAR

long bit[&#173;32&#093;

PUB Start | i, x

repeat i from 0 to 31
bit[&#173;i&#093; := |< i

x := $38
i := 4
if (x & bit[&#173;i&#093;)
'...
elseif (x & bit1)
'...





-Phil

Rayman
07-14-2009, 03:50 AM
I think a real easy way is use OUTA (assuming that cog isn't really being used for output...).

You can just do:

OUTA:=$38

if OUTA[bit]
...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm

ericball
07-14-2009, 08:47 PM
Phil Pilgrim (PhiPi) said...
Here are a couple other ways: one which uses an array, for when the bit number is a variable; the other, using constants, for when the bit number is a constant.



if (x & bit[­i])
'...
elseif (x & bit1)
'...




I'd have to look at the SPIN bytecode, but I suspect "if (x & |<i)" will be faster than "if (x & bit[i])".· It also saves 32 LONGs of HUB RAM.


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Composite NTSC sprite driver: Forum (http://forums.parallax.com/showthread.php?p=800114)
NTSC & PAL driver templates: Forum (http://forums.parallax.com/showthread.php?p=803904)
OnePinTVText driver: ObEx (http://obex.parallax.com/objects/480/) Forum (http://forums.parallax.com/showthread.php?p=822453)