# Python Bit Functions on Integer

Bit Functions in Python:

Before we begin with Python bit functions for integers, let us first understand their interconversion.

Conversion of data values enters the picture when we are either automating a manual procedure or working with system-level information.

While dealing with data in various numeric forms such as hexadecimal, numeric, octal, and so on, bit functions play a crucial part in analyzing the bit level data of integers.

Python provides the following collection of bit level methods to assist us evaluate integer data in terms of bit level information and representation:

• bit_length() function
• to_bytes() function
• int.from_bytes() function

## Bit Functions on Integer in Python

#### 1)bit_length() function:

int.bit_length() returns the number of bits needed to represent an integer in binary, ignoring the sign and leading zeros.

For example:

Let the Number = 2

Its binary representation = 0010.

The actual length of its binary representation  = 4

But the int.bit_length() function returns the output = 2

It excludes or ignores the two leading zeroes, it only counts the non-zero places and returns the length as 2.

Hence the output = 2

2) Number = -7

However, because the function ignores the sign value, it handles it as if it were any other positive integer.

Output = 3

1)For Example

Approach:

• Give the number as static input and store it in a variable.
• Apply bit_length() function on the given number that returns the number of bits needed to represent an integer in binary, ignoring the sign and leading zeros.
• Store it in another variable.
• Print the above result.
• The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 2
# Apply bit_length() function on the given number that returns the number
# of bits needed to represent an integer in binary, ignoring the sign and
# Store it in another variable.
rslt_len = gvn_numb.bit_length()
# Print the above result.
print("The result length for given number{", gvn_numb, "} =", rslt_len)


Output:

The result length for given number{ 2 } = 2
# Similarly, do for the other numbers and print the result
gvn_numb = 5
print(gvn_numb.bit_length())

gvn_numb = -7
print(gvn_numb.bit_length())


Output:

3
3

#### 2)to_bytes() function:

Return a byte array representing an integer.

• When byteorder is set to “big,” the most significant byte is at the start of the byte array.
• The most important byte is at the end of the byte array if byteorder is set to “little.”

The signed argument checks if the two’s complement is used to represent the integer.

Syntax:

int.to_bytes(length, byteorder, signedorder = False)

Parameter values

length: It is the length of the resulting array.

byteorder: It may be “big” or “little”.

signedorder: When set to True, it uses two’s complement to represent the number as a bytes array.

1)For Example

# This function returns the byte representation of 1024 in big endian machine
# Here, we have expressed the integer value 1024 as a 4-byte array (length)
# with the most significant byte at the beginning of the array.
print((1024).to_bytes(4, byteorder='big'))


Output:

b'\x00\x00\x04\x00'

#### 3)int.from_bytes() function:

This function returns the integer represented by the specified array of bytes.

In other words, the from_bytes() method accepts an array of bytes as an argument, along with the byteorder parameter, and returns the integer value corresponding to it.

Syntax:

int.from_bytes(bytes, byteorder, signed=False)

For Example:

# from_bytes() function Returns a big endian integer value of 'x00x10'.
print(int.from_bytes(b'\x00\x00\x04\x00', byteorder='big'))


Output:

1024