Shop OBEX P1 Docs P2 Docs Learn Events
Why is their no Bitwise AND NOT in BS2 — Parallax Forums

Why is their no Bitwise AND NOT in BS2

RickHRickH Posts: 40
edited 2008-05-23 17:20 in BASIC Stamp
I'm trying to determin a bit changing in a byte of data recived from an IC2 chip.

I'm writing a debug rotine to test it.
DebugCode:
  RANDOM DebugVar1
  DebugVar2 = %10101010
  DebugVar3 = Debugvar1 | DebugVar2
  DebugVar4 = DebugVar1 & DebugVar2
  DEBUG "Var1: ",BIN8 DebugVar1,"   Var1: ",BIN8 DebugVar1," ",CR
  DEBUG "Var1: ",BIN8 DebugVar2,"   Var1: ",BIN8 DebugVar2," ",CR
  DEBUG "OR:   ",BIN8 DebugVar3,"   AND : ",BIN8 DebugVar4," ",CR
  FOR idx = 7 TO 0
  storage1 = debugvar1 << idx
  storage1 = storage1 >> 7
  storage2 = debugvar2 << idx
  storage2 = storage2 >> 7
  changed = storage1 ^ storage2
  bitWentHigh = storage1 [color=red]&/[/color] storage2
  DEBUG BIN storage1," ",BIN storage2," ",BIN ? changed," ",BIN ? bitwenthigh,CR
  NEXT
  PAUSE 16384
  DEBUG CLS
RETURN


the &/ only works with a BS1?

how would I do this in a BS2?

This seams to work, but don't seam optimal.
' {$STAMP BS2p}
' {$PBASIC 2.5}

DebugVar1          VAR      Byte
DebugVar2          VAR      Byte
storage1           VAR      Byte
storage2           VAR      Byte
results            VAR      Byte
Bit_Changed         VAR      Bit
BitWentHigh        VAR      Bit
idx                VAR      Nib
Bitreg             VAR      Nib
setup:
DebugVar1 = %10101010
main:
  GOSUB DebugCode
  GOTO main
DebugCode:
  Bitreg = 0
  RANDOM DebugVar1
  DebugVar2 = %10101010
  DEBUG "Var1: ",BIN8 DebugVar1,"   Var1: ",BIN8 DebugVar1,"   Var1: ",BIN8 DebugVar1," ",CR
  DEBUG "Var2: ",BIN8 DebugVar2,"   Var2: ",BIN8 DebugVar2,"   Var2: ",BIN8 DebugVar2," ",CR
  DEBUG "  OR: ",BIN8 (Debugvar1 | DebugVar2),"    AND: ",BIN8 (DebugVar1 & DebugVar2),"    XOR: ",BIN8 (DebugVar1 ^ DebugVar2)," ",CR
  FOR idx = 7 TO 0
    storage1 = debugvar1 << idx
    storage1 = storage1 >> 7
    storage2 = debugvar2 << idx
    storage2 = storage2 >> 7
    bit_Changed = storage1 ^ storage2
    IF bit_changed = 1  THEN
      IF storage1 > storage2 THEN
        BitwentHigh = 1
      ELSE
        BitWenthigh = 0
      ENDIF
    ELSE
      BitWentHigh = 0
    ENDIF
    DEBUG BIN storage1," - ",BIN storage2," - Bit ",DEC Bitreg," Changed: ",BIN bit_changed," - Bit ",DEC Bitreg, " Went HIGH: ",BIN bitwenthigh,CR
    IF Bit_Changed = 0 AND BitWentHigh = 1 THEN
      DEBUG "ERROR"
    ENDIF
  Bitreg = Bitreg+1
  NEXT
  PAUSE 32768
  DEBUG HOME
RETURN


Post Edited (RickH) : 5/21/2008 9:34:50 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-21 13:02
    How about "BitWentHigh = storage1 & ! storage2"?
  • RickHRickH Posts: 40
    edited 2008-05-22 06:08
    Their is no operator "!" and it shows in the manual that their isnt any of the following bitwise operators in any BS2 chips
    &/ Bitwise AND NOT
    |/ Bitwise OR NOT
    ^/ Bitwise XOR NOT
    I was just wondering why.
  • Tracy AllenTracy Allen Posts: 6,667
    edited 2008-05-22 06:48
    On the Stamp 2, bitwise not is the tilda ~. It is unary and takes precedence over binary operators, so you can write those three bitwise functions as,

    z = x &~ y ' bitwise AND NOT
    z = x |~ y ' bitwise OR NOT
    z = x ^~ y ' bitwise XOR NOT

    For detecting a change from low to high in the bits of a variable I usually use the following function:

    changedHigh = newvalue ^ oldvalue & newvalue

    The only 1's in changedHigh will be bits that were 0 in oldvalue and have become 1 in newvalue. That can process 16 bits at once in parallel or a byte or nib or single bit. Say it is a byte, then

    IF changedHigh.bit0(5) THEN GOSUB action

    will take the action if bit 5 has changed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • RickHRickH Posts: 40
    edited 2008-05-23 17:20
    Ahhh, thanks. I can be a bit dence sometimes lol. I haven't worked with low level operations in quite a while and sometimes the obvious bits me in the but.
Sign In or Register to comment.