binary

Logical OR for Bit-string in Python

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-13 03:17:04
问题 What i want to do is have the result of logical OR for two bit-strings. For example: a='010010' b='000101' c=LOGIC_OR(a,b) c 010111 The error i encounter most of the time is when I convert 'b' from string to binary it removes leading zeros. Others methods i have used convert 'a' and 'b' to integers. Generally nothing is working and help would be much appreciated. Thanks in advance 回答1: Here are a couple of alternative methods. Third-party bitarray library: from bitarray import bitarray a=

Converting Decimal to Binary, how to get 8-bit binary?

点点圈 提交于 2020-11-29 09:49:19
问题 So I need to convert decimal to binary but all of my returns need to be 8 bits long. I've already figured out the conversion itself but I need to figure out how to add zero's to the beginning if it's less than 8 bits. old_number = int(input("Enter whole number here: ")) number = old_number binary_translation = 0 while number > -1: if number > 128 or number == 128: binary_translation = binary_translation + 10000000 number = number - 128 elif number > 64 or number == 64: binary_translation =