Shop OBEX P1 Docs P2 Docs Learn Events
How can I flip bits in SX/B? — Parallax Forums

How can I flip bits in SX/B?

T&E EngineerT&E Engineer Posts: 1,396
edited 2009-05-22 00:49 in General Discussion
If I have a variable tmpB3 which·let's say looks·something like this: %11001010 and I want the bits to be fliped so it now is: %01010011

How can this be done?



More specifically·I have a data statement with a character (or really lots of characters in DATA statements):

UC_Letter_A:
DATA· %01111100 'A
DATA· %00010010
DATA· %00010001
DATA· %00010010
DATA· %01111100

and I don't want to have to retype every character in flipped again but I need this:

UC_Letter_A:
DATA· %00111110·'A (flipped bits)
DATA· %01001000
DATA· %10001000
DATA· %01001000
DATA· %00111110

There should be a way to read it in and flip the bits. How can this be done in SX/B 2.0?

Thanks for your help!
·

Comments

  • ZootZoot Posts: 2,227
    edited 2009-05-21 01:37
    You mean "reverse" the bit order? There is no command for it. Unrolling a loop is probably the most compact way to do it...

    
    READ someAddr, tmpB99
    
    tmpB3.0 = tmpB99.7
    tmpB3.1 = tmpB99.6
    tmpB3.2 = tmpB99.5
    tmpB3.3 = tmpB99.4
    tmpB3.4 = tmpB99.3
    tmpB3.5 = tmpB99.2
    tmpB3.6 = tmpB99.1
    tmpB3.7 = tmpB99.0
    
    
    



    Of course, you can write a function with two bit masks if you need to operate on a wide swatch of variables with variable reverse counts and lengths (a la the Stamp REV operator), or you can use two other temps or a local stack and load 'em up as parameters...

    
    FUNC revBits
    __PARAM2.0 = __PARAM1.7
    __PARAM2.1 = __PARAM1.6
    __PARAM2.2 = __PARAM1.5
    __PARAM2.3 = __PARAM1.4
    __PARAM2.4 = __PARAM1.3
    __PARAM2.5 = __PARAM1.2
    __PARAM2.6 = __PARAM1.1
    __PARAM2.7 = __PARAM1.0
       RETURN __PARAM2
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • BeanBean Posts: 8,129
    edited 2009-05-21 02:34
    DEVICE SX28, OSC4MHZ 
    FREQ 4_000_000 
    
     
    origData VAR BYTE 
    flipData VAR BYTE 
    
     
    PROGRAM Start NOSTARTUP 
    
     
    Start: 
      origData = %11001010 
      ASM 'flipData = OrigData with bits flipped; NOTE: Destoys value in origData 
        RR origData 
        RL flipData 
        RR origData 
        RL flipData 
        RR origData 
        RL flipData 
        RR origData 
        RL flipData 
        RR origData 
        RL flipData 
        RR origData 
        RL flipData 
        RR origData 
        RL flipData 
        RR origData 
        RL flipData 
      ENDASM 
    END
    

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...

    ·
  • ZootZoot Posts: 2,227
    edited 2009-05-21 03:25
    Nice. "C"lever use of C flag.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2009-05-21 14:51
    Thank you guys very much!

    I thought about Zoot's first approach but was locked into thinking I could use a FOR NEXT loop which failed as seen in another post from PJ Allen.

    e.g.

    j = 7

    FOR i = 0 TO 7

    tmp4.j = tmp3.i

    j=j-1

    NEXT

    However, sometimes you need to break it down to the simplest form like Zoot did. This is working but I have other minor issues to deal with (which I will try to work out before I ask for help).

    I am writing code for the new 0832 Display boards and have it almost working but these boards use nibbles not bytes so it's a bit awkward: http://www.sureelectronics.net/goods.php?id=903

    PS: Is there any advantage (e.g. significant speed difference or saving ram space, etc.) using Bean's assembly language routine as I am not familiar with this as much as SX/B?

    Thanks again guys!
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-05-21 15:23
  • ZootZoot Posts: 2,227
    edited 2009-05-21 17:11
    Bean's code is way more compact... var.0 = var.7 compiles to a MOVB which is 4 instructions per bit or 32 instructions to re-map a byte. Bean's code is half that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • JonnyMacJonnyMac Posts: 9,213
    edited 2009-05-22 00:49
    I wrote a library of bit-oriented routines for the SX -- in it you'll find functions that will reverse the bits in a byte and in a word.

    ' Use: result = FLIP_BYTE aByte
    ' -- flips bits in byte end-to-end
    
    FUNC FLIP_BYTE
      '{$IFUSED FLIP_BYTE}
      ASM
        MOV   __PARAM2, __PARAM1                    ' make copy to __PARAM2
        CLR   __PARAM3                              ' clear count
    
    FB_Loop:
        RR    __PARAM2                              ' get bit from __PARAM2
        RL    __PARAM1                              ' move into __PARAM1
        INC   __PARAM3                              ' update count
        JNB   __PARAM3.3, @FB_Loop                  ' abort at 8
      ENDASM
      '{$ENDIF}
      ENDFUNC
    



    ' Use: result = FLIP_WORD aWord
    ' -- flips bits in word end-to-end
    
    FUNC FLIP_WORD
      '{$IFUSED FLIP_WORD}
      ASM
        MOV   __PARAM3, __PARAM1                    ' make copy to __WPARAM34
        MOV   __PARAM4, __PARAM2
        CLR   __PARAM5                              ' clear count
    
    FW_Loop:
        RR    __PARAM4                              ' get bit from __WPARAM34
        RR    __PARAM3
        RL    __PARAM1                              ' move into __WPARAM12
        RL    __PARAM2
        INC   __PARAM5                              ' update count
        JNB   __PARAM5.4, @FW_Loop                  ' abort at 16
      ENDASM
      '{$ENDIF}
      ENDFUNC
    

    Post Edited (JonnyMac) : 5/22/2009 12:56:56 AM GMT
Sign In or Register to comment.