Convert Bytes To Bits in Python
Last Updated :
12 May, 2025
Converting bytes to bits in Python involves representing each byte in its binary form, where each byte is composed of 8 bits. For example , a byte like 0xAB (which is 171 in decimal) would be represented as '10101011' in binary. Let’s explore a few techniques to convert bytes to bits in Python.
Using int.from_bytes(...).bit_length()
This method converts a byte sequence to an integer using int.from_bytes() and then calculates the number of bits required to represent that integer using .bit_length().
Python
a = b'\x01\x23\x45\x67\x89\xAB\xCD\xEF'
b = int.from_bytes(a, byteorder='big').bit_length()
print(b)
Explanation: int.from_bytes(a, byteorder='big').bit_length() converts a byte sequence a to an integer (big-endian) and returns the number of bits needed to represent that integer.
Using generator expression
This method counts the number of 1 bits in each byte of the byte sequence by converting each byte to its binary form and counting the occurrences of 1 using a generator expression and bin(byte).count('1').
Python
a = b'\x01\x23\x45\x67\x89\xAB\xCD\xEF'
b = sum(bin(byte).count('1') for byte in a)
print(b)
Explanation: sum(bin(byte).count('1') for byte in a) converts each byte in a to binary, counts the number of 1 bits and sums them up.
Using bitwise shift
This approach constructs the integer by shifting each byte into its correct position using bitwise left shift (<<) and OR (|). After constructing the full integer, it calculates the bit length using .bit_length().
Python
a = b'\x01\x23\x45\x67\x89\xAB\xCD\xEF'
val = 0
for byte in a:
val = (val << 8) | byte
b = val.bit_length()
print(b)
Explanation: for byte in a: val = (val << 8) | byte constructs an integer val by shifting it left by 8 bits for each byte and OR'ing the byte. After all bytes are processed, val.bit_length() returns the number of bits needed to represent val.
Using binary string join
This method creates a binary string by converting each byte to its 8-bit binary representation and joining them. Then, it calculates the bit length by measuring the length of the binary string after stripping leading zeros.
Python
a = b'\x01\x23\x45\x67\x89\xAB\xCD\xEF'
binary_str = ''.join(f"{byte:08b}" for byte in a)
b = len(binary_str.lstrip('0'))
print(b)
Explanation: ''.join(f"{byte:08b}" for byte in a) converts each byte in a to its 8-bit binary form and concatenates them into one string. binary_str.lstrip('0') removes leading zeros and len(binary_str) calculates the number of remaining bits.
Similar Reads
Convert Bytearray To Bytes In Python In Python, dealing with binary data is a common task, and understanding how to convert between different binary representations is crucial. One such conversion is from a bytearray to bytes. In this article, we will explore five simple methods to achieve this conversion, along with code examples for
3 min read
How to Convert Bytes to Int in Python? Converting bytes to integers in Python involves interpreting a sequence of byte data as a numerical value. For example, if you have the byte sequence b'\x00\x01', it can be converted to the integer 1.Using int.from_bytes()int.from_bytes() method is used to convert a byte object into an integer. It a
3 min read
How to Convert Int to Bytes in Python? The task of converting an integer to bytes in Python involves representing a numerical value in its binary form for storage, transmission, or processing. For example, the integer 5 can be converted into bytes, resulting in a binary representation like b'\x00\x05' or b'\x05', depending on the chosen
2 min read
Convert Hex String to Bytes in Python Converting a hexadecimal string to bytes in Python involves interpreting each pair of hexadecimal characters as a byte. For example, the hex string 0xABCD would be represented as two bytes: 0xAB and 0xCD. Letâs explore a few techniques to convert a hex string to bytes.Using bytes.fromhex() bytes.fro
2 min read
How to Convert Bytes to String in Python ? We are given data in bytes format and our task is to convert it into a readable string. This is common when dealing with files, network responses, or binary data. For example, if the input is b'hello', the output will be 'hello'.This article covers different ways to convert bytes into strings in Pyt
2 min read