Smaller than a Byte?
Basil
Posts: 380
Hi All,
Just wondering if there is anyway to declare a global variable smaller than a byte? I need a variable to be bit sized (for true/false), and I also need·a 2 bit variable.
Thanks!
Alec
Just wondering if there is anyway to declare a global variable smaller than a byte? I need a variable to be bit sized (for true/false), and I also need·a 2 bit variable.
Thanks!
Alec
Comments
Graham
if you wanted to have 8 x 1bit variables for example
you could do :-
CON
variable1 = $80
variable2 = $40
variable3 = $20
variable4 = $10
variable5 = $08
variable6 = $04
variable7 = $02
variable8 = $01
VAR
byte variables
'in following examples replace variableX with variable1 to 8 depending on which variable you want to use
'then to set variable
variables := variables | variableX
'and to clear the variable
variables := variables and ( !variableX )
'and to test the variable
if variables & variableX
Hope this helps,
Baggers.
On the Stamps, with the very very small variable space, you're always trying to save a bit here and there and program space is "free" unless your program is large and complex. On the Propeller, variable space and program space come from the same pool. Use a whole byte or even a long for simple variables. The only exceptions should be large arrays. You'll use a lot of space for the complex messing around you have to do to pack several values into a byte. If you do need to pack stuff into a word or long word, say for eventual use off-chip, consider making an object to do the packing and unpacking with a "get" method and a "set" method for each bitfield and a subscript as a parameter for bit arrays.
So I could use a byte instead of 8 individual bit's (how obvious is that huh lol)
Silly question, but how do I access individual bits in a variable?
Thanks guys!
You use conventional Boolean arithmetic (&, |, !) and the shift operators (<<, >>, |<) like:
1) To set a bit "n": x |= |< n
2) To clear a bit "n": x &= ! |< n
3) To toggle a bit "n": x ^= |< n
4) To test for bit "n" true: IF x & |< n <> 0
5) To test for bit "n" false: IF x & |< n == 0
Just saying [noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://forums.parallax.com/showthread.php?p=650217
meow, i have my own topic now? (sorta)
I do have another quick question about the variable 'n' in Mikes equations.
Can I do the following:
CON
Bool1name := 0 '1st bit in the byte
Bool2name := 1 '2nd bit in the byte
Bool7name := 6 '6th bit in the byte
etc etc
and use Boolxname (or whatever) in place of N?
Eg. To test for bit "1" true: IF x & |< Bool7name <> 0
#0, FirstFlag, Debug, Red, Green, Blue, Bit5, FlagName, Eyes, Nose, Mouth, Throat, Ears 'continue up to 32 entries or less
Then when isolating the bit use |< FlagName or whatever your enumated bit and use this in Mike's boolean arithmatic.
So lets say Flags is your flag "container"
Flags |= |< Nose
would set the 7th bit which is your Nose flag.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Post Edited (Paul Baker (Parallax)) : 5/24/2007 2:22:09 AM GMT
Just treat them as bytes and set them as true-false (remember that "true" in propellerland means -1, in this case $FF, not 1)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://forums.parallax.com/showthread.php?p=650217
meow, i have my own topic now? (sorta)
Post Edited (M. K. Borri) : 5/24/2007 2:31:10 AM GMT
This looks familiar though I don't know where ive seen it lol does that line go in the DAT section or VAR?
So if I follow you correctly...
To test for bit "1" in the boolen value called Rec_Decend true:
Bools holds the true/falses for all the boolen var's
Flag |= |< Rec_Decend 'Rec_Decend is set in that #0 line above
IF bools & |< Flag <> 0
.....
yeah, I just feel bad using a whole byte for a single bit lol
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Thanks for the explanation [noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Sorry to bring this one up again, but how would I go about doing the same for a 2bit variable? (4 variables per byte)
I may actually end up using a full byte for every variable as some have suggested, but I would be interested to know this for future reference
Thanks!
As you might imagine, there are a variety of ways to do this for 2-bits (or 3-bits or 4-bits or ...). If it were me, I'd define two constants for each field. One would be the bitstring involved and the other would be the number of the rightmost bit. If the field were called FOO, I'd define FOO_BITS and FOO_POS. These might be FOO_BITS = %00001100 and FOO_POS = 2. I would use the bit operators and the shift operators to access the bitstring. For example, to isolate and insert a new integer value into the field, I'd do "val := (val & ! FOO_BITS) | (newint << FOO_POS)". Other operations are an exercise for the reader...
Thanks Mike!
then to get the mask you simply use tmask << (hoof << 1)
the parenthesed quantity multiplys the enumerated placeholder by 2 (since thats the number of bits in a tayste), then shifts the mask of 2 bits (the number 3). You use this generated mask to clear the field, set the field or shift values into the proper bit positions by doing "value << (hoof << 1)".
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Its all easier than expected!