1

I have a list A = [100011, 1110110]. I want to make each entry in the list 8 bit using the format option:

B = '{0:08b}'.format(A[1])
B = '{0:08b}'.format(A[2])

But when I print(B). I am getting the output as: B = 100001111000001011110 and B = 11000011010101011

Why is this happening? Is this command not used for list?

1 Answer 1

2

The integer literals defined in the list A are specified in decimals. You should specify them as binary literals (prefixed with 0b) instead:

A = [0b100011, 0b1110110]
print('{0:08b}'.format(A[0]))
print('{0:08b}'.format(A[1]))

This outputs:

00100011
01110110
Sign up to request clarification or add additional context in comments.

2 Comments

As with all formatting options, if the result doesn't fit in the specified length the length is expanded. Good catch on the constants.
@MarkRansom Thank you. That helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.