-1

I need to take an input (ranging from 1-12) from the user and store the data as binary. (cannot use arrays) For example: if the user inputs 3, it would return 000000000100. (the 3rd digit from the right)

I was thinking that this would be possible with a log algorithm, but I don't really know where to start. How would I do this? Any help is appreciated.

Olivier
  • 13,283
  • 1
  • 8
  • 24
eth
  • 3
  • 2
  • ... Do you want to return the string `"000000000100"`, or the binary value `100`, which is the number 4? – khelwood Sep 09 '21 at 15:03
  • 1
    `1 << (n-1)` will calculate that value – Barmar Sep 09 '21 at 15:04
  • @khelwood I need it to be 12 bytes, as this will take multiple inputs, so 000000000100 – eth Sep 09 '21 at 15:05
  • 1
    What **type** of value do you want to return? A string? An int? An array? – khelwood Sep 09 '21 at 15:07
  • The goal is for it to be an int that can be read similarly to an array. By this I mean that the user can input multiple numbers and the corresponding byte will change to a 1. eg. 1 = 000000000001; inputs 2 and one would be 000000000011 – eth Sep 09 '21 at 15:09
  • Does this answer your question? [Does Python have a bitfield type?](https://stackoverflow.com/questions/142812/does-python-have-a-bitfield-type) – Stef Sep 09 '21 at 15:58

2 Answers2

0

If you want to update any index to 1 multiple times across multiple inputs, you might want to use a list containing 12 elements that you can tick. Then with that list, you can already get both the string value e.g. "000000000100" and the int value e.g. 4

# Initialize list that we will tick
bin_digits = ["0"] * 12

# Let's assume that the input from user is from 1 to 12
for num in range(1, 13):
    # Tick the target index
    bin_digits[-num] = "1"

    bin_str = "".join(bin_digits)  # String value
    bin_int = int(bin_str, 2)  # Int value

    print(bin_digits, bin_str, bin_int)

Output

['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1'] 000000000001 1
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1'] 000000000011 3
['0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1'] 000000000111 7
['0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1'] 000000001111 15
['0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1'] 000000011111 31
['0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1'] 000000111111 63
['0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1'] 000001111111 127
['0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1'] 000011111111 255
['0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1'] 000111111111 511
['0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'] 001111111111 1023
['0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'] 011111111111 2047
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'] 111111111111 4095
0

If I am reading this problem correctly you are being fed in numbers 1-12 and depending on what numbers are fed in you need to return a binary string where the bits at whatever positions are given are equal to one (without lists/arrays). To achieve this you could read in values to a set and then construct a string where the inputted values are one and everything else is zero. Like this:

def read_in():
    positions = set()
    while True:
        print('Enter 1-12 or Q to stop:',end=' ')
        entry = input()
        if entry != 'Q':
            positions.add(int(entry))
        else:
            break
    ret = ''
    for i in range(12,0,-1):
        if i in positions:
            ret += '1'
        else:
            ret += '0'
    return ret
print(read_in())
0x263A
  • 1,807
  • 9
  • 22