Mirror imaging binary numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig

    Mirror imaging binary numbers

    Hi there,

    I'm trying to switch binary numbers around so that the MSB becomes the
    LSB etc. Is there an easy way of doing this as I can't seem to find
    anything. If you could help that would be great. Thanks and good
    luck.


    Craig

  • Matimus

    #2
    Re: Mirror imaging binary numbers

    Craig wrote:
    I'm trying to switch binary numbers around so that the MSB becomes the
    LSB etc.
    What do you mean 'binary numbers'? They are all binary. If you mean the
    int type, they are 32 bits long and there are 16 bits between the MSB
    and LSB (Most/Least Significant _Byte_). Do you want to swap the most
    significant word with the least significant word? Swap the most
    significant nibble with the least significant nibble in a Byte? Or do
    you want to completely reverse the bit order?

    To swap nibbles in a byte:

    reverseVal = (val & 0xf) << 4 | (val & 0xf0) >4

    Basicly you are going to need to use the bit operators (|,&, << and >>)
    to get what you need. If you could be more specific perhaps I could be
    of more help.

    Comment

    • Craig

      #3
      Re: Mirror imaging binary numbers


      Matimus wrote:
      Craig wrote:
      I'm trying to switch binary numbers around so that the MSB becomes the
      LSB etc.
      >
      What do you mean 'binary numbers'? They are all binary. If you mean the
      int type, they are 32 bits long and there are 16 bits between the MSB
      and LSB (Most/Least Significant _Byte_). Do you want to swap the most
      significant word with the least significant word? Swap the most
      significant nibble with the least significant nibble in a Byte? Or do
      you want to completely reverse the bit order?
      >
      To swap nibbles in a byte:
      >
      reverseVal = (val & 0xf) << 4 | (val & 0xf0) >4
      >
      Basicly you are going to need to use the bit operators (|,&, << and >>)
      to get what you need. If you could be more specific perhaps I could be
      of more help.
      Thanks so much for the response. I have an array of individual bytes
      which will eventually make up a binary bitmap image that is loaded onto
      an LCD screen (1 = black dot, 0 = white dot). At the moment each byte
      is reversed to what it should be (completely reverse the bit order):
      e.g 00111101 should be 10111100, 11001100 should be 00110011, etc. It
      is not an int problem as such, it is more a bit level swap if you get
      what I mean. If you could help that would be great.

      Comment

      • Grant Edwards

        #4
        Re: Mirror imaging binary numbers

        On 2006-12-06, Craig <craigtw.online @gmail.comwrote :
        Thanks so much for the response. I have an array of
        individual bytes which will eventually make up a binary bitmap
        image that is loaded onto an LCD screen (1 = black dot, 0 =
        white dot). At the moment each byte is reversed to what it
        should be (completely reverse the bit order): e.g 00111101
        should be 10111100, 11001100 should be 00110011, etc. It is
        not an int problem as such, it is more a bit level swap if you
        get what I mean. If you could help that would be great.
        He's already told you 90% of the answer: use the bit operators
        & | ~ ^ ><<.

        Here's the remaining 10% of the answer (done a couple different
        ways):

        def showbits8(b):
        mask = 0x80
        while mask:
        print "01"[(b & mask) != 0],
        mask >>= 1
        print

        def bitswap8a(b):
        r = 0
        mask = 0x80
        while mask:
        r >>= 1
        if b & mask:
        r |= 0x80
        mask >>= 1
        return r

        def bitswap8b(b):
        r = 0
        for m1,m2 in ((0x80,0x01),(0 x40,0x02),(0x20 ,0x04),(0x10,0x 08),(0x01,0x80) ,(0x02,0x40),(0 x04,0x20),(0x08 ,0x10)):
        if b & m1:
        r |= m2
        return r


        def testit(b):
        showbits8(b)
        showbits8(bitsw ap8a(b))
        showbits8(bitsw ap8b(b))
        print

        testit(0xc1)
        testit(0x55)
        testit(0xe2)




        --
        Grant Edwards grante Yow! Is this "BOOZE"?
        at
        visi.com

        Comment

        • mensanator@aol.com

          #5
          Re: Mirror imaging binary numbers


          Craig wrote:
          Matimus wrote:
          >
          Craig wrote:
          I'm trying to switch binary numbers around so that the MSB becomes the
          LSB etc.
          What do you mean 'binary numbers'? They are all binary. If you mean the
          int type, they are 32 bits long and there are 16 bits between the MSB
          and LSB (Most/Least Significant _Byte_). Do you want to swap the most
          significant word with the least significant word? Swap the most
          significant nibble with the least significant nibble in a Byte? Or do
          you want to completely reverse the bit order?

          To swap nibbles in a byte:

          reverseVal = (val & 0xf) << 4 | (val & 0xf0) >4

          Basicly you are going to need to use the bit operators (|,&, << and >>)
          to get what you need. If you could be more specific perhaps I could be
          of more help.
          >
          Thanks so much for the response. I have an array of individual bytes
          which will eventually make up a binary bitmap image that is loaded onto
          an LCD screen (1 = black dot, 0 = white dot). At the moment each byte
          is reversed to what it should be (completely reverse the bit order):
          e.g 00111101 should be 10111100, 11001100 should be 00110011, etc. It
          is not an int problem as such, it is more a bit level swap if you get
          what I mean. If you could help that would be great.
          >>import gmpy # GNU Multi-precision library for Python
          >>for i in xrange(16):
          s = gmpy.digits(i,2 ) # turn int to a base 2 string
          p = '0'*(8-len(s)) + s # pad out leading 0's
          r = ''.join(reverse d(p)) # reverse it
          print p,r # voila


          00000000 00000000
          00000001 10000000
          00000010 01000000
          00000011 11000000
          00000100 00100000
          00000101 10100000
          00000110 01100000
          00000111 11100000
          00001000 00010000
          00001001 10010000
          00001010 01010000
          00001011 11010000
          00001100 00110000
          00001101 10110000
          00001110 01110000
          00001111 11110000

          Comment

          • dickinsm@gmail.com

            #6
            Re: Mirror imaging binary numbers

            On Dec 6, 6:01 pm, "Craig" <craigtw.onl... @gmail.comwrote :
            Thanks so much for the response. I have an array of individual bytes
            which will eventually make up a binary bitmap image that is loaded onto
            an LCD screen (1 = black dot, 0 = white dot). At the moment each byte
            is reversed to what it should be (completely reverse the bit order):
            e.g 00111101 should be 10111100, 11001100 should be 00110011, etc. It
            is not an int problem as such, it is more a bit level swap if you get
            what I mean. If you could help that would be great.
            Yet another solution:

            def flipbits(x):
            """reverse bits in a byte"""
            x1 = x << 4 | x >4
            x2 = (x1 & 51) << 2 | (x1 & 204) >2
            return (x2 & 85) << 1 | (x2 & 170) >1

            The idea is to first swap the two nybbles, then swap bits 0, 1, 5, 6
            with 2, 3, 6, 7 respectively,
            and finally swap bits 0, 2, 4, 6 with bits 1, 3, 5, 7 respectively.

            Mark

            Comment

            • Grant Edwards

              #7
              Re: Mirror imaging binary numbers

              On 2006-12-06, dickinsm@gmail. com <dickinsm@gmail .comwrote:
              Yet another solution:
              >
              def flipbits(x):
              """reverse bits in a byte"""
              x1 = x << 4 | x >4
              x2 = (x1 & 51) << 2 | (x1 & 204) >2
              return (x2 & 85) << 1 | (x2 & 170) >1
              >
              The idea is to first swap the two nybbles, then swap bits 0,
              1, 5, 6 with 2, 3, 6, 7 respectively, and finally swap bits 0,
              2, 4, 6 with bits 1, 3, 5, 7 respectively.
              It's a little less obtuse if you spell it this way:

              def flipbits(x):
              """reverse bits in a byte"""
              x1 = x << 4 | x >4
              x2 = (x1 & 0x33) << 2 | (x1 & 0xcc) >2
              return (x2 & 0x55) << 1 | (x2 & 0xaa) >1

              --
              Grant Edwards grante Yow! Now I understand the
              at meaning of "THE MOD SQUAD"!
              visi.com

              Comment

              • dickinsm@gmail.com

                #8
                Re: Mirror imaging binary numbers



                On Dec 6, 7:20 pm, Grant Edwards <gra...@visi.co mwrote:
                It's a little less obtuse if you spell it this way:
                >
                def flipbits(x):
                """reverse bits in a byte"""
                x1 = x << 4 | x >4
                x2 = (x1 & 0x33) << 2 | (x1 & 0xcc) >2
                return (x2 & 0x55) << 1 | (x2 & 0xaa) >1
                >
                Granted. And a little more obtuse this way:

                def flipbits(x):
                """reverse bits in a byte"""
                x += 255*(x & 15)
                x += 15*(x & 816)
                x += 3*(x & 5440)
                return x >7

                I apologise---it's the end of a long day and I'm feeling more than a
                little contrary.

                Mark

                Comment

                • Terry Reedy

                  #9
                  Re: Mirror imaging binary numbers


                  "Craig" <craigtw.online @gmail.comwrote in message
                  news:1165446077 .584437.206310@ 73g2000cwn.goog legroups.com...
                  Thanks so much for the response. I have an array of individual bytes
                  which will eventually make up a binary bitmap image that is loaded onto
                  an LCD screen (1 = black dot, 0 = white dot). At the moment each byte
                  is reversed to what it should be (completely reverse the bit order):
                  e.g 00111101 should be 10111100, 11001100 should be 00110011, etc. It
                  is not an int problem as such, it is more a bit level swap if you get
                  what I mean. If you could help that would be great.
                  Using any of the solutions posted by others, I would first make a 256 byte
                  string in which each byte was the bit reversed version of its index.

                  IE, bitrev = "\x00\x80\x40\x C0.....\xFF"

                  Then your actual image processing is a simple, quick lookup for each byte.

                  Terry Jan Reedy



                  Comment

                  • Craig

                    #10
                    Re: Mirror imaging binary numbers


                    Terry Reedy wrote:
                    "Craig" <craigtw.online @gmail.comwrote in message
                    news:1165446077 .584437.206310@ 73g2000cwn.goog legroups.com...
                    Thanks so much for the response. I have an array of individual bytes
                    which will eventually make up a binary bitmap image that is loaded onto
                    an LCD screen (1 = black dot, 0 = white dot). At the moment each byte
                    is reversed to what it should be (completely reverse the bit order):
                    e.g 00111101 should be 10111100, 11001100 should be 00110011, etc. It
                    is not an int problem as such, it is more a bit level swap if you get
                    what I mean. If you could help that would be great.
                    >
                    Using any of the solutions posted by others, I would first make a 256 byte
                    string in which each byte was the bit reversed version of its index.
                    >
                    IE, bitrev = "\x00\x80\x40\x C0.....\xFF"
                    >
                    Then your actual image processing is a simple, quick lookup for each byte.
                    >
                    Terry Jan Reedy
                    Thanks for all your great help guys. They work great.

                    Comment

                    • Hendrik van Rooyen

                      #11
                      Re: Mirror imaging binary numbers

                      "Craig" <craigtw.online @gmail.comwrote :

                      Hi there,
                      >
                      I'm trying to switch binary numbers around so that the MSB becomes the
                      LSB etc. Is there an easy way of doing this as I can't seem to find
                      anything. If you could help that would be great. Thanks and good
                      luck.
                      Are these Python ints, or are they "strings of bytes of known length"?
                      And do you really want to mirror swap the binary bits or just change the
                      byte sequence from say big to little endian?
                      - i.e. is MSB most significant bit, or byte?

                      assuming bit, try this:
                      >>num = 12345
                      >>q,r = divmod(num,2)
                      >>q
                      6172
                      >>r
                      1
                      >>l = [r]
                      >>while q:
                      q,r = divmod(q,2)
                      l.append(r)

                      >>l
                      [1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1]
                      >>ans = 0
                      >>for x in l:
                      ans = ans * 2 + x

                      >>ans
                      9987
                      >>>
                      and Robert is yer aunty...

                      - Hendrik

                      Comment

                      Working...