0

I have a system verilog function I am trying to convert into python. the function takes a binary file and reads in and does some ECC function which checks the if the input is valid.

function int local_ecc_function(int word_in) ;
    int ecc;
ecc[0] = word_in[0 ]^ word_in[1 ]^ word_in[3 ]^ word_in[4 ]^ word_in[6 ]^ word_in[8 ]^ word_in[10 ]^ word_in[11 ]^ word_in[13 ]^ word_in[15 ]^ word_in[17 ]^ word_in[19 ]^ word_in[21 ]^ word_in[23 ]^ word_in[25 ]^ word_in[26 ]^ word_in[28 ]^ word_in[30];
...

my python reads the file and converts it into a list of ints:

bytes_arr = f.read(4)
list_temp = []
int_temp = int.from_bytes(bytes_arr, byteorder='big')
while bytes_arr:
    bytes_arr = f.read(4)
    int_temp = int.from_bytes(bytes_arr, byteorder='big')        
    list_temp.append(int_temp)

how can I convert the int into 32bits list so I can execute the ECC function? I am using python 3.8

Gilad
  • 6,437
  • 14
  • 61
  • 119
  • 1. The function seems to want a 32-bit _int_, not a list. 2. In your code, what you have is a list, which I guess you want as an int, to pass to your function. So a bit confused by _"how can I convert the int into 32bits list so I can execute the ECC function?"_ Could you post what `list_temp` looks like after your loop so that we can help with converting that. – aneroid Jan 19 '21 at 11:42
  • @aneroid i can't post my list, the goal is to iterate over the list of ints i have created and convert them into bits. each int. – Gilad Jan 19 '21 at 11:48

2 Answers2

3

As seen here (https://stackoverflow.com/a/10322018/14226448) you can convert an int to a bit array like such:

def bitfield(n):
    return [int(digit) for digit in bin(n)[2:]] # [2:] to chop off the "0b" part

If you want to get a bool array you can then just cast that into a bool like such:

def bitfield(n):
    return [bool(int(digit)) for digit in bin(n)[2:]] # [2:] to chop off the "0b" part
Camaendir
  • 472
  • 3
  • 8
1

One liner from list_temp to array of bools:

[bool(int(b)) for num in list_temp for b in bin(num)[2:]]

Other things you could convert to:

# without an example of `list_temp`, let's assume it's a list of 4 8-bit ints (<255):
list_temp = [72, 101, 108, 112]

# converting to a string of bits:
bit_str = ''.join(bin(n)[2:] for n in list_temp)
# if you need '112' to be the most-significant, change the line above to:
bit_str = ''.join(bin(n)[2:] for n in list_temp[::-1])

# as an array of bools:
[bool(int(b)) for b in bit_str]
# one-liner version above

# and if you want that all as one large int made up of 1's and 0's:
int(bit_str)

# Output:
# 1001000110010111011001110000

# And if you want what 32-bit integer the above bit-string would be:
int(bit_str, base=2)
# 152663664

Edit: Somehow missed the 'bool array' in the title.


Since you're on Python 3.8, let's clean up that reading a bit; and utilise Assignment Expressions while we're at it:

list_temp = []
while bytes_arr := f.read(4):
    int_temp = int.from_bytes(bytes_arr, byteorder='big')
    list_temp.append(int_temp)

# or use a while-True block and break if bytes_arr is empty.
aneroid
  • 12,983
  • 3
  • 36
  • 66
  • i would like to convert 32bits int not 8 bits – Gilad Jan 19 '21 at 12:33
  • 1
    **From what?** You haven't posted an example of `list_temp`. We're "guessing" it's a list of 4 8-bit ints - so you can get one 32-bit int. If you want help on how to _read_ the binary file to create a 32-bit int, then provide an example of that file and the expected result. – aneroid Jan 19 '21 at 12:43