Shop OBEX P1 Docs P2 Docs Learn Events
byte and one bit in the byte — Parallax Forums

byte and one bit in the byte

Zap-oZap-o Posts: 452
edited 2010-02-10 15:00 in Propeller 1
I am wondering how to go about this. Say I have a byte "Numb" and I want to manipulate bit 6 in the byte.

for example:

Numb := %1111_0000

Now I want to make all the bits in Numb the same except bit 6.

Numb := %1011_0000

I would prefer to have 2 variables i.e. Numb and the NewNumb

NewNumb := %1

Numb := %11newNumb1_0000

I hope this makes since.

Comments

  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-02-10 13:41
    Numb[noparse][[/noparse]6] := newNumb


    EDIT: SORRY THIS IS GARBAGE FOR BYTES

    Post Edited (Graham Stabler) : 2/10/2010 3:02:15 PM GMT
  • Zap-oZap-o Posts: 452
    edited 2010-02-10 13:48
    Graham is that a byte array? I am using a serial to parallel IC so I can only use a byte.
  • BradCBradC Posts: 2,601
    edited 2010-02-10 13:50
    Graham Stabler said...
    Numb[noparse][[/noparse]6] := newNumb

    Bit indexing like that only works on SFR's.. If Numb was defined as a byte, that would write the byte at @Numb+6 instead.

    Off the top of my head.. something like..

    Numb := (Numb & !(1<<6)) | (NewNumb <<6)

    .. might work

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.

    Post Edited (BradC) : 2/10/2010 1:56:28 PM GMT
  • localrogerlocalroger Posts: 3,452
    edited 2010-02-10 14:02
    Bitwise Decode is your friend. !< 6 == %0100_0000 and you can use a var or expression instead of the number 6.

    Turn bit 6 on: numb |= |< 6

    "numb or-equals bitwise-decode six"

    Turn bit 6 off: numb &= ! |< 6

    "numb and-equals bitwise-not bitwise-decode six"
  • Zap-oZap-o Posts: 452
    edited 2010-02-10 14:11
    Boy my head is spinning now.

    I cant understand that stuff you put down localroger. Please give me a few more minutes and write down a little more details. I am even reading the propeller manual (bitwise decode) and I am lost. I guess its got something to do with me going to work at 2am.
  • BradCBradC Posts: 2,601
    edited 2010-02-10 14:21
    Zap-o said...
    Boy my head is spinning now.

    I cant understand that stuff you put down localroger. Please give me a few more minutes and write down a little more details. I am even reading the propeller manual (bitwise decode) and I am lost. I guess its got something to do with me going to work at 2am.

    It's really easy once you get your head around it..

    (|< 6) would give you the same result of (1 << 6) but uses less code space and does it quicker.

    In plain english, you give it a number between 0 & 31, and it gives you a long with only that particular bit set.

    So to set the bit in a long, you would use xx |= |< bit
    |< bit generates a long with only the single bit set, and or's it with the existing value (setting that bit unconditionally)

    To unset the bit you would use xx &= ! |< bit

    the ! |< bit generates the long with the single bit set, and the ! inverts it to create a long with all bits except that one set. If you and that with the original value, you get the original value without that bit set.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
  • Zap-oZap-o Posts: 452
    edited 2010-02-10 14:28
    So should I declare it as a long or can I keep it as a byte?
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-02-10 14:30
    Have a look at the logic-tables of bitwise "and" and bitwise "or". If you want to set a bit, you would simply use bitwise or.

    newVal := old Val "bitwise or" bit-pattern

    You get the bit-pattern of a single bit by using |<x operation, where x is the number of the bit you want to set.

    If you want to clear a bit, then you can use "bitwise and" where all bits are 1 except the one you want to clear.

    new Val := old Val "bitwise and" with bit-pattern where bit to clear is 0

    This bit-pattern can be generated by inverting the |<x, which is !|<x

    localroger simply used the shortcut of assigning operators, which do the bitwise or/and of left and righthand operand and directly assign the result back to the lefthand-operand.
    (|= and &=)


    PS: I should switch off all interrupts, when answering posts ;o)

    Post Edited (MagIO2) : 2/10/2010 2:36:22 PM GMT
  • BradCBradC Posts: 2,601
    edited 2010-02-10 14:30
    Zap-o said...
    So should I declare it as a long or can I keep it as a byte?

    No a byte is fine.

    All operations in the spin interpreter are carried out as long. If you declare a byte, when the operation is written out, it is simply truncated down to the declared size.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
  • Zap-oZap-o Posts: 452
    edited 2010-02-10 14:46
    Sorry guys it works but not for other bits. It erases the last state. so if I want bit 6 to remain as a %1 and the I wish to make bit 5 a %1 bit 6 is lost.
  • Zap-oZap-o Posts: 452
    edited 2010-02-10 14:57
    Solved. Thanks for the code brad

    Pub main
          PortData := BitTrick (PortData,3,1)
          PortData := BitTrick (PortData,4,0) 
          ...do stuff
    
    Pub BitTrick (In,Bit,OnOff) : Out
       Out := (In & !(1<<bit)) | (OnOff << Bit) 
    
    



    This works because I can set any bit at any time then send it out to the serial to parallel IC.
  • BradCBradC Posts: 2,601
    edited 2010-02-10 15:00
    Ahh, I was just writing a reply when your second post came through.. Nice work!

    A trivial change, based on localrogers concept above :

      Out := (In & ! |< bit) | (OnOff << Bit)
    
    



    That would be marginally faster an a poofteenth smaller.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
Sign In or Register to comment.