Python Forum
Help in breaking bytes into bits through masking and shifting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help in breaking bytes into bits through masking and shifting
#7
You can encode this by using ordinary integer operations
def encode(sign, x, y):
    assert 0 <= x <= 2
    assert 0 <= y <= 9
    assert sign in (0, 1)
    return 64 * sign + 16 * x + y

def decode(n):
    m, y = divmod(n, 16)
    sign, x = divmod(m, 4)
    return sign, x, y

if __name__ == '__main__':
    from itertools import product
    for s, x, y in product(range(2), range(3), range(10)):
        n = encode(s, x, y)
        print(s, x, y, bin(n), decode(n))
Output:
0 0 9 0b1001 (0, 0, 9) 0 1 0 0b10000 (0, 1, 0) 0 1 1 0b10001 (0, 1, 1) 0 1 2 0b10010 (0, 1, 2) 0 1 3 0b10011 (0, 1, 3) 0 1 4 0b10100 (0, 1, 4) 0 1 5 0b10101 (0, 1, 5) 0 1 6 0b10110 (0, 1, 6) 0 1 7 0b10111 (0, 1, 7) 0 1 8 0b11000 (0, 1, 8) 0 1 9 0b11001 (0, 1, 9) 0 2 0 0b100000 (0, 2, 0) 0 2 1 0b100001 (0, 2, 1) 0 2 2 0b100010 (0, 2, 2) 0 2 3 0b100011 (0, 2, 3) 0 2 4 0b100100 (0, 2, 4) 0 2 5 0b100101 (0, 2, 5) 0 2 6 0b100110 (0, 2, 6) 0 2 7 0b100111 (0, 2, 7) 0 2 8 0b101000 (0, 2, 8) 0 2 9 0b101001 (0, 2, 9) 1 0 0 0b1000000 (1, 0, 0) 1 0 1 0b1000001 (1, 0, 1) 1 0 2 0b1000010 (1, 0, 2) 1 0 3 0b1000011 (1, 0, 3) 1 0 4 0b1000100 (1, 0, 4) 1 0 5 0b1000101 (1, 0, 5) 1 0 6 0b1000110 (1, 0, 6) 1 0 7 0b1000111 (1, 0, 7) 1 0 8 0b1001000 (1, 0, 8) 1 0 9 0b1001001 (1, 0, 9) 1 1 0 0b1010000 (1, 1, 0) 1 1 1 0b1010001 (1, 1, 1) 1 1 2 0b1010010 (1, 1, 2) 1 1 3 0b1010011 (1, 1, 3) 1 1 4 0b1010100 (1, 1, 4) 1 1 5 0b1010101 (1, 1, 5) 1 1 6 0b1010110 (1, 1, 6) 1 1 7 0b1010111 (1, 1, 7) 1 1 8 0b1011000 (1, 1, 8) 1 1 9 0b1011001 (1, 1, 9) 1 2 0 0b1100000 (1, 2, 0) 1 2 1 0b1100001 (1, 2, 1) 1 2 2 0b1100010 (1, 2, 2) 1 2 3 0b1100011 (1, 2, 3) 1 2 4 0b1100100 (1, 2, 4) 1 2 5 0b1100101 (1, 2, 5) 1 2 6 0b1100110 (1, 2, 6) 1 2 7 0b1100111 (1, 2, 7) 1 2 8 0b1101000 (1, 2, 8) 1 2 9 0b1101001 (1, 2, 9)
Reply


Messages In This Thread
RE: Help in breaking bytes into bits through masking and shifting - by Gribouillis - Nov-11-2020, 09:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 386 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,200 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Openpyxl module breaking my code EnderSM 5 1,163 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,289 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to rotate bits ? korenron 2 1,689 Mar-23-2022, 04:05 PM
Last Post: Larz60+
  How to use += after breaking for loop? Frankduc 2 1,537 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  From list of bits to PDF drimades 1 1,966 Nov-02-2021, 08:55 PM
Last Post: Gribouillis
  How to fix while loop breaking off raphy11574 3 2,270 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  breaking a large program into functions, not acting as expected Zane217 9 3,107 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,407 Sep-04-2021, 04:51 PM
Last Post: imillz

Forum Jump:

User Panel Messages

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