1

Let's say I have A = 0b11110101.

I want to make a new integer like this pseudocode

B[0] = A[4]
B[1] = A[7]
B[2] = A[0]
B[3] = A[6]
B[4] = A[1]
B[5] = A[2]
B[6] = A[3]
B[7] = A[5]

How can I do that?

Maybe I can create an array B first, but at the end I need an integer/binary.

Update #1

I made something like this:

num=0
pc1_tidyHex = []

pc1_tidyHex.append([tidyHex[56], tidyHex[48], tidyHex[40], tidyHex[32], tidyHex[24], tidyHex[16], tidyHex[8]])
num = int(''.join(map(str, pc1_tidyHex)))    
print(pc1_tidyHex)
print(num)

and I got this error:

Traceback (most recent call last):
  File "F:\TUC Aux\python\budi.py", line 47, in <module>
    num = int(''.join(map(str, pc1_tidyHex)))
ValueError: invalid literal for int() with base 10: "['0', '1', '1', '0', '1', '0', '0']"
1

3 Answers 3

4

convert integer A into binary string from LSB to MSB then manipulate the bit with indexing

bin_map = [5,3,2,1,6,0,7,4]
A = 0b11110101
binA = bin(A)[2:][::-1]
B = int(''.join(binA[i] for i in bin_map), 2)
# B will be integer 175 or binary 0b10101111
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your idea. I think I can use this idea on my next step. What should I do if I want variable B to be an array?
To keep B as array, just replace the last line of my code become B = [binA[i] for i in bin_map]
1

Once you are done with your array you can use:

num = int(''.join(map(str, B)))

and you will have your integer. If you want it as string, remove the int()

5 Comments

Hi, I got an error, I updated my post above. That is the only way that I know how to build an array, this is my first doing python.
@Codelearner777 this is because you are appending an array to the array. In that case you can change to num = int(''.join(pc1_tidyHex[0]))
And about creating an array, you can directly create the array with the values: pc1_tidyHex = [tidyHex[56], tidyHex[48], tidyHex[40],..etc] but then you need to change to num = int(''.join(pc1_tidyHex))
This answer doesn't operate in a bitwise manner. you want to use the base argument of int.
@Codelearner777 Don't forget to accept the answer if you find it useful =)
0

You cannot use string join to turn an array of bits into an integer. You need to do some bit shifting:

A = 0b11110101

b_map = [5, 3, 2, 1, 6, 0, 7, 4]  # map of bits from your `A`, high->low order

B = 0  # initialize as 0
for target_position in b_map:  # loop through our positions
    B = (B << 1) | (A >> target_position & 1)  # shift by one, pick the target bit from A

print("B: {} (binary: {})".format(B, bin(B)))
# B: 175 (0b10101111)

Not exactly sure if that's what you're attempting to do, tho, your question is a bit confusing.

1 Comment

Sure you can, though it's convoluted and inefficient: int(''.join(str(e) for e in array), 2) compared to something like sum(v<<i for i,v in enumerate(array))

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.