Shop OBEX P1 Docs P2 Docs Learn Events
Conversion of Byte to 8 Bits — Parallax Forums

Conversion of Byte to 8 Bits

John RitzJohn Ritz Posts: 14
edited 2005-01-22 00:01 in BASIC Stamp
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!

Comments

  • NewzedNewzed Posts: 2,503
    edited 2005-01-21 16:23
    You write"



    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
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-21 16:28
    All variables, except bits, have bit modifiers. Sid showed how you can alias them above; you can also use them directly in code.

    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
  • John RitzJohn Ritz Posts: 14
    edited 2005-01-21 17:45
    Thanks! That is nice!
    So if I want to get bits 0 - 7 of a byte variable I can just do this:

    myByte VAR BYTE
    idx VAR BYTE
     
    myByte = 85
     
    for idx = 0 to 7
       if myByte.LOWBIT(idx) = 1 then
          'do something here...
       endif
    next
    
    

    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:

    myByte VAR BYTE
    idx VAR BYTE
     
    myByte = 85
     
    if myByte.bit0 = 1 then
       'do something here...
    endif
     
    if myByte.bit1 = 1 then
       'do something here...
    end if
    
    

    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! tongue.gif

    Thanks...John
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-22 00:01
    No, you would always use LOWBIT (there is a HIGHBIT modifier, but it can be confusing) -- with a Word variable you would have an index value of 0 to 15.

    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
Sign In or Register to comment.