B4R Question Bits rotation

Discussion in 'B4R Questions' started by Mostez, Jul 1, 2019.

  1. Mostez

    Mostez Active Member

    I want to to rotate byte-bits, the resulting byte-bit-0 = source byte-bit7 and so on for next 7 bits
    if i have a byte = 00001001 it should be = 10010000 after rotation, any ideas?
  2. Erel

    Erel Administrator Staff Member Licensed User

    I'm not sure that I understand. Can you post more examples?
  3. thetahsk

    thetahsk Active Member Licensed User

    This is right bit rotation, the bits that are fall out at the right end are put back at left end.

    Examples in c,c++,c#.java
    Mostez likes this.
  4. emexes

    emexes Well-Known Member Licensed User

    That looks more like a reflection.

    A rotation to the right would be 10000100

    A rotation to the left would be 00010010

    If you are looking for a rotation, it sounds like you want it to be to the left, since you say bit 7 is transferred to bit 0. The easiest way to do that would be:

    X = 0x09    'binary 00001001 = hexadecimal 09 = decimal 9

    X = X + X    
    'shift X one bit to the left (ie multiply-by-two) so X is now binary 000010010 = decimal 18
    if X >= 256 then    'if bit 8 (just shifted out of bit 7) is set then
        X = X - 255        'clear bit 8 (subtract 256) and put it into bit 1 instead (add 1)
    End If
    Last edited: Jul 2, 2019
  5. Mostez

    Mostez Active Member

    I have font byte array, when I send font to graphics lcd, characters printed upside down, so i need to re-arrange bits in opposite order, what i need is like;
    NewByte(bit0) = OldByte(bit7)
    NewByte(bit1) = OldByte(bit6)
    NewByte(bit7) = OldByte(bit0)
  6. emexes

    emexes Well-Known Member Licensed User

    How about this?...
    Dim ReflectByte(256)

    For B0 = 0 to 1
    For B1 = 0 to 1
    For B2 = 0 to 1
    For B3 = 0 to 1
    For B4 = 0 to 1
    For B5 = 0 to 1
    For B6 = 0 to 1
    For B7 = 0 to 1
        ReflectByte(B0 * 
    128 + B1 * 64 + B2 * 32 + B3 * 16 + B4 * 8 + B5 * 4 + B6 * 2 + B7) = B7 * 128 + B6 * 64 + B5 * 32 + B4 * 16 + B3 * 8 + B2 * 4 + B1 * 2 + B0

    'and then you can just use that array as a lookup table:

    NewByte = ReflectByte(OldByte)
  7. emexes

    emexes Well-Known Member Licensed User

    Or this:
    Sub ReflectByte(X As Int) As Int

    Dim OldByte as Int = Bit.And(X, 255)    'partly because I can't remember whether X is passed by value or reference
        Dim NewByte As Int = 0

    For B = 1 to 8    '8 bits
            NewByte = NewByte + NewByte    'shift left
            If Bit.And(OldByte, 1) <> 0 Then
                NewByte = NewByte + 
    1    'set bit 0
            End If
            OldByte = 
    Bit.ShiftRight(OldByte, 1)

    Return NewByte

    End Sub

    For I = 0 to 255
    Log(I & " reflected = " & ReflectByte(I))
  8. Mostez

    Mostez Active Member

    here is what I did, it works OK,
    Dim tmB As Byte = 0
    Dim B As Byte
    Select Reflect
                tmB = cByte
    For B = 0 To 7
    If Bit.Get(cByte,B) = 1 Then Bit.Set(tmB,7 - B)
    End Select
    Last edited: Jul 4, 2019
    emexes likes this.
  9. emexes

    emexes Well-Known Member Licensed User

    Indeed, that is simpler than my code, so:

    :) :) :)

    I'd like to test the relative speeds, but I don't have the Bit.Set and Bit.Get methods in the version of B4A that I'm running at the moment. I can see that they would be useful additions.
    Last edited: Jul 4, 2019
    Mostez likes this.
  10. emexes

    emexes Well-Known Member Licensed User

    Just realised we're in the B4R forum.

    With a bit of luck, those Bit methods might percolate through to B4A/J/I too.
  11. emexes

    emexes Well-Known Member Licensed User

    If you're interested, a faster way might be:
    Sub ReflectByte(X As Int) As Int

    Dim Y As Int = X

    'bitmasks: 0x55 = 01010101   0x33 = 00110011   0x0F = 00001111
        '          0xAA = 10101010   0xCC = 11001100   0xF0 = 11110000
        Y = 
    Bit.Or( Bit.ShiftLeft(Bit.And(Y, 0x55), 1), Bit.ShiftRight(Bit.And(Y, 0xAA), 1) )  'swap alternate 1-bit blocks
        Y = Bit.Or( Bit.ShiftLeft(Bit.And(Y, 0x33), 2), Bit.ShiftRight(Bit.And(Y, 0xCC), 2) )  'swap alternate 2-bit blocks
        Y = Bit.Or( Bit.ShiftLeft(Bit.And(Y, 0x0F), 4), Bit.ShiftRight(Bit.And(Y, 0xF0), 4) )  'swap alternate 4-bit blocks (nybbles within bytes)
    Return Y

    End Sub
    Presumably it'd be even faster in B4R if you changed the Ints to Bytes (whereas in Java, anything smaller than an Int is handled as an Int) (broadly speaking)

    The advantage of this way is that each doubling of the data size, from 8 bits to 16 bits to 32 bits, adds just one line execution, rather than doubling, ie efficiency is O(log(n)) rather than O(n).
    Last edited: Jul 4, 2019
    Mostez likes this.
  12. Mostez

    Mostez Active Member

    first, thank you so much for your valuable replies, you're right, I replaced the code in post #8
  13. emexes

    emexes Well-Known Member Licensed User

    I feel like you've had it under control from the beginning anyway, but... if I knew how to put a thumbs-up emoji here, I would. Several, in fact!

    I assume you'll be reflecting/reversing the bitmaps in the vertical direction too, but presumably that's a lot easier, especially if the bitmaps are 8 pixels wide (or some multiple thereof).
  14. techknight

    techknight Well-Known Member Licensed User

    if you can do in-line ASM, you could simply just ror the register.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice