Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate bits in Python

Tags:

python

I have two bytes, e.g. 01010101 and 11110000. I need to concatenate the four most significant bit of the second byte "1111" and the first whole byte, resulting something like 0000010101011111, namely, padding four zeros, the first whole byte and finally the four most significant bit of the second byte.

Any idea?

like image 784
john518 Avatar asked Oct 31 '13 11:10

john518


People also ask

How do you concatenate bits?

The concatenation operator "{ , }" combines (concatenates) the bits of two or more data objects. The objects may be scalar (single bit) or vectored (muliple bit). Mutiple concatenations may be performed with a constant prefix and is known as replication. module Concatenation (A, B, Y);

How do you concatenate bytes in Python?

The easiest way to do what you want is a + a[:1] . You could also do a + bytes([a[0]]) . There is no shortcut for creating a single-element bytes object; you have to either use a slice or make a length-one sequence of that byte.

How do you append binary numbers in Python?

We will first convert the binary string to a decimal using int() function in python. The int() function in Python and Python3 converts a number in the given base to decimal. Then we will add it and then again convert it into a binary number using bin() function.


1 Answers

Try this:

first = 0b01010101
second = 0b11110000
res = (first<<4) | (second>>4)
print bin(res)

By shifting the first byte by 4 bits to the left (first<<4) you'll add 4 trailing zero bits. Second part (second>>4) will shift out to the right 4 LSB bits of your second byte to discard them, so only the 4 MSB bits will remain, then you can just bitwise OR both partial results (| in python) to combine them.

Splitting result back

To answer @JordanMackie 's question, you can split the res back to two variables, just you will loose original 4 least significant bits from second.

first = 0b01010101
second = 0b11110000
res = (first<<4) | (second>>4)
print ("res    : %16s" %(bin(res)) )

first2 = (res>>4) & 255
second2 = (res&0b1111)<<4

print ("first2 : %16s" % (bin(first2)) )
print ("second2: %16s" % (bin(second2)) )

Output looks like this:

res    :    0b10101011111
first2 :        0b1010101
second2:       0b11110000

First of the commands extracts original first byte. It shifts 4 LSB bits that came from second variable to the right (operator >>), so they will be thrown away. Next logical and operation & keeps only 8 lowest bits of the operation and any extra higher bits are thrown away:

first2 = (res>>4) & 255

Second of the commands can restore only 4 MSB bits of the second variable. It selects only 4 LSB from the result that belong to second using logical multiplication (&). (anything & 1 = anything, anything & 0 = 0). Higher bits are discarded because they are AND'ed with 0 bit.

Next those 4 bits are shifted to the left. Zero bits appear at 4 lowest significant bit positions:

second2 = (res&0b1111)<<4
like image 186
nio Avatar answered Oct 08 '22 09:10

nio