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
#10
(Nov-16-2020, 12:29 PM)Gribouillis Wrote: You need something more systematic, perhaps along the lines of
def encode(pattern, numbers):
    result = 0
    for size, n in zip(pattern, numbers):
        result = (result << size) + n
    return result

def decode(pattern, n):
    result = []
    for size in reversed(pattern):
        n, r = divmod(n, 1 << size)
        result.append(r)
    return tuple(reversed(result))


if __name__ == '__main__':
    pat_lat = (1, 3, 4 ,4, 4, 4, 4)
    pat_long = (1, 3, 3, 4, 4, 3, 4, 4)
    data = (1, 2, 5, 5, 7, 6, 3)
    n = encode(pat_lat, data)
    print(data)
    print(bin(n))
    print(decode(pat_lat, n))
Output:
(1, 2, 5, 5, 7, 6, 3) 0b101001010101011101100011 (1, 2, 5, 5, 7, 6, 3)
Hi Sir, I know this is long due and I am so sorry for that, but is there a way to encode and decode the latitude and longitude of the GPS in such a way that it transmits lesser bits?
Reply


Messages In This Thread
RE: Help in breaking bytes into bits through masking and shifting - by kamui123 - Jan-11-2021, 07:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 355 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,117 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Openpyxl module breaking my code EnderSM 5 1,123 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,266 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to rotate bits ? korenron 2 1,667 Mar-23-2022, 04:05 PM
Last Post: Larz60+
  How to use += after breaking for loop? Frankduc 2 1,514 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  From list of bits to PDF drimades 1 1,950 Nov-02-2021, 08:55 PM
Last Post: Gribouillis
  How to fix while loop breaking off raphy11574 3 2,245 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  breaking a large program into functions, not acting as expected Zane217 9 3,061 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,392 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