Shop OBEX P1 Docs P2 Docs Learn Events
Smaller than a Byte? — Parallax Forums

Smaller than a Byte?

BasilBasil Posts: 380
edited 2007-05-27 21:55 in Propeller 1
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

Comments

  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-22 12:35
    if you use a byte it can still be 1 or 0 you just won't be using all of the bits. The savings from using a smaller variable if it existed would barely be worth it.

    Graham
  • BaggersBaggers Posts: 3,019
    edited 2007-05-22 13:00
    Hi Basil

    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.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-22 13:07
    Although this would increase memory usage by one byte?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-22 13:42
    Graham,
    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.
  • BasilBasil Posts: 380
    edited 2007-05-22 21:12
    Hmmm thanks for all the input(s) [noparse]:)[/noparse]
    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!
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-22 21:46
    Basil,
    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
  • BasilBasil Posts: 380
    edited 2007-05-22 22:28
    So simple! Thanks Mike [noparse]:)[/noparse] This will save alot of memory space! lol
  • M. K. BorriM. K. Borri Posts: 279
    edited 2007-05-24 00:58
    Unless you have a huge array though, the extra code to do bitwise is going to eat up space -- it may be more space than you're saving by doing bitwise (that happened to me at some point).

    Just saying [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://forums.parallax.com/showthread.php?p=650217

    meow, i have my own topic now? (sorta)
  • BasilBasil Posts: 380
    edited 2007-05-24 01:14
    Well, I have maybe 12 true/false type variables so not too many [noparse]:)[/noparse]

    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
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-24 02:17
    Basil, you may want to use enumeration for each bit.

    #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
  • M. K. BorriM. K. Borri Posts: 279
    edited 2007-05-24 02:25
    If you only have 12 variables, I would just use a byte for each... you can use all the inbuilt operations without the extra >> and << and so on, and eventually this means you _save_ memory, also it'll be a little faster. This is if they are global variables, which seems to me is the case.

    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
  • BasilBasil Posts: 380
    edited 2007-05-24 02:43
    Paul Baker (Parallax) said...

    #0, FirstFlag, Debug, Red, Green, Blue, Bit5, FlagName, Eyes, Nose, Mouth, Throat, Ears

    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
    .....
    M. K. Borri said...
    If you only have 12 variables, I would just use a byte for each... you can use all the inbuilt operations without the extra >> and << and so on, and eventually this means you _save_ memory, also it'll be a little faster.

    yeah, I just feel bad using a whole byte for a single bit lol
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-24 02:56
    It a string of constants in ascending order so it goes in the CON section.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • BasilBasil Posts: 380
    edited 2007-05-24 03:01
    Ahhh thanks, and what does the #0 mean? I assume it means the constants start at number 0? (Rather than 1)

    Thanks for the explanation [noparse]:)[/noparse]
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-24 03:07
    Basil said...
    Ahhh thanks, and what does the #0 mean? I assume it means the constants start at number 0? (Rather than 1)

    Thats correct.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • BasilBasil Posts: 380
    edited 2007-05-24 03:12
    Ahh cool [noparse]:)[/noparse] thanks guys!
  • BasilBasil Posts: 380
    edited 2007-05-25 04:30
    Hi Guys,

    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!
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-25 04:47
    Basil,
    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...
  • BasilBasil Posts: 380
    edited 2007-05-25 04:52
    Hmmm ok I think I understand [noparse]:)[/noparse]

    Thanks Mike!
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-25 05:17
    You can use the same type of enumeration for tydbit (aka tayste, crumb or morsel)
    CON
     tmask = 3
     #0, tayste, tydbit, crumb, morsel, hoof, twobits
    
    

    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.
  • BasilBasil Posts: 380
    edited 2007-05-27 21:55
    Great! Thanks paul [noparse]:)[/noparse]

    Its all easier than expected!
Sign In or Register to comment.