Python Forum
Help Understanding Some Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Understanding Some Code
#1
I'm trying to understand this code:
def readSize(self):
        b1 = ord(self.read(1))
        if b1 & 0b10000000:  # 1 byte
            return b1 & 0b01111111
        elif b1 & 0b01000000:  # 2 bytes
            return unpack(">H", bchr(b1 & 0b00111111) + self.read(1))[0]
        elif b1 & 0b00100000:  # 3 bytes
            return unpack(">L", b"\0" + bchr(b1 & 0b00011111) + self.read(2))[0]
        elif b1 & 0b00010000:  # 4 bytes
            return unpack(">L", bchr(b1 & 0b00001111) + self.read(3))[0]
        elif b1 & 0x00001000:  # 5 bytes
            return unpack(">Q", b"\0\0\0" + bchr(b1 & 0b00000111) + self.read(4))[0]
        elif b1 & 0b00000100:  # 6 bytes
            return unpack(">Q", b"\0\0" + bchr(b1 & 0b0000011) + self.read(5))[0]
        elif b1 & 0b00000010:  # 7 bytes
            return unpack(">Q", b"\0" + bchr(b1 & 0b00000001) + self.read(6))[0]
        elif b1 & 0b00000001:  # 8 bytes
            return unpack(">Q", b"\0" + self.read(7))[0]
        else:
            assert b1 == 0
raise EbmlException("undefined element size")
I've a basic knowledge of bitwise operators via Google. I will get into more depth later. I just don't understand what this code is doing. What is
0b1000000
I know it's a binary number, but what does the "0b" represent? I've seen "0x" prefacing binary numbers (though I don't really get what it means either), but never "0b".
I would appreciate any help decoding this for me.

Thanks,
malonn
Reply
#2
0b indicates that the number is in a binary representation and 0x in a hexadecimal representation.

>>> 0b011
3
>>> 0x011
17
>>> 
011 in binary is: 2²*0 + 2¹*1 + 2⁰ * 1 = 3 (decimal)
011 in hexa is: 16²*0 + 16¹*1 + 16⁰ * 1 = 17 (decimal)
Reply
#3
Ah, gotcha. Thank you @gontajones. I have read about converting base-2 and base-16 numbers to base-10, and it is essential in working with bytes and byte objects in Python. Good stuff.

That's a positive step in understanding the above code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code understanding: Need help in understanding dictionary code jt123 0 450 Jul-09-2023, 01:13 PM
Last Post: jt123
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,664 May-09-2023, 08:47 AM
Last Post: buran
  Understanding a piece of code Michael1 4 1,373 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Beginner: I need help understanding few lines of a code. hop_090 1 1,642 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 1,406 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  Help Understanding Code Variables 1 1,884 May-02-2019, 05:53 PM
Last Post: micseydel
  Need help understanding simple Array code. Please. stluwa 1 2,185 Apr-13-2019, 07:16 PM
Last Post: loomski
  Trouble Understanding Why This Code Works crocolicious 2 2,663 Apr-09-2019, 05:24 PM
Last Post: crocolicious
  Help Understanding Portion of Code caroline_d_124 3 2,679 Jan-15-2019, 12:12 AM
Last Post: caroline_d_124
  Understanding GerbMerge code djsb 1 2,355 Oct-27-2018, 02:04 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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