Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to rotate bits ?
#1
Hello,
I have a function that take bits and cut them according to my needs
BITS_ORDER_CHANGE = {8: 0, 7: 1, 6: 2, 5: 3, 4: 4, 3: 5, 2: 6, 1: 7}

def GetBits(Byte, start, end):  # bits order 8765 4321 , str order 0123 4567
    Final_calculation = round((int(Byte, 16) * 1 + 0), 0)
    bits = "{0:b}".format(Final_calculation)
    Full_bits = bits.zfill(8)
    print('old start', start)
    start = BITS_ORDER_CHANGE[start]
    print('new start', start)
    print('old end', end)
    end = BITS_ORDER_CHANGE[end]
    print('new end', end)
    Wanted_bits = Full_bits[end:start]
    return Wanted_bits
my problem is that when I cut it I think is string mode , whie the bits are is in bits mode
so it's other way around

for example
if I take "CA"
in bits its - 1100 1010
I want to cut only bits 2-4
but I need to enter numbers 4-7

then I thought to use dictionary - but then it's went wrong
I need to change the order - (end - start) , and it also change the order of the bits
01 - change to 10

is there other way to do it? without overthinking what need to be cut?


Thanks,
Reply
#2
Don't convert to a string. Use shift and bitwise and. The convention I am familiar with has bit order starting at 0, not 1.
import math

def bit_slicer(value, lsb, msb=None):
    """Slice bits[lsb:msb] from value.  Return as int"""
    if msb is None:
        msb = lsb
    if msb < lsb:
        raise ValueError("msb must be greater or equal to lsb")
    mask = (2 << (msb-lsb)) - 1
    return (value >> lsb) & mask

def bits(value, lsb=0, msb=None):
    """Return bits[lsb:msb] from value.  Return as list"""
    if msb is None:
        msb = int(math.log(value, 2))
    if msb < lsb:
        raise ValueError("msb must be greater or equal to lsb")
    return [1 if value & (1 << bit) else 0 for bit in range(lsb, msb+1)]

print(bit_slicer(0xCA, 2, 4))
print(bits(0xCA, 2, 4))
print(bits(0xCA))
Output:
2 [0, 1, 0] [0, 1, 0, 1, 0, 0, 1, 1]
But if you want to stay with doing this the string way, just reverse the order of the bit string.
import math

def bits(value, lsb=0, msb=None):
    """Return bits[lsb:msb] from value.  Return as list"""
    if msb is None:
        msb = int(math.log(value, 2))
    if msb < lsb:
        raise ValueError("msb must be greater or equal to lsb")
    bits = f"{value:b}".zfill(msb)[::-1]  # Reverse the order
    return [int(bit) for bit in bits[lsb:msb+1]]

print(bits(0xCA))
Output:
[0, 1, 0, 1, 0, 0, 1, 1]
codinglearner likes this post
Reply
#3
see: https://wiki.python.org/moin/BitwiseOperators
and: https://wiki.python.org/moin/BitManipulation
and: https://wiki.python.org/moin/BitArrays

The second link has many examples
codinglearner likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,069 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  From list of bits to PDF drimades 1 1,921 Nov-02-2021, 08:55 PM
Last Post: Gribouillis
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,590 Jan-11-2021, 07:42 AM
Last Post: kamui123
  Rotate 2D Gaussian given parameters schniefen 4 2,927 Dec-11-2020, 03:34 PM
Last Post: schniefen
  How to rotate log and limit the size maiya 0 1,763 Aug-29-2020, 12:41 AM
Last Post: maiya
  rounding floats to a number of bits Skaperen 2 2,322 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  Problem with Pycharm 64 bits sylas 12 10,263 Jan-22-2018, 02:19 PM
Last Post: sparkz_alot
  send bits to the signal generator liorjo 2 3,120 Dec-20-2017, 07:46 PM
Last Post: liorjo
  How to convert python files from 32 bits tto 64 bits sylas 2 5,094 Oct-29-2017, 03:51 AM
Last Post: Larz60+
  Why are two similar bits of code giving different results? godmode 10 9,973 Dec-15-2016, 09:53 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020