Basically I'm trying to grab 1's and 0's values from an array and perform bitwise operations on that.
board = np.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
for x in board:
s = str(x)
s = int(s)
This is obviously not correct however. This has its own binary value and if I perform bitwise operations on it (eg. >>) I'm shifting the underlying binary.
So how can I dynamically create binary strings to perform bitwise operations on?
Any help appreciated.
CodePudding user response:
board = np.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
for x in board:
s = str(x)
s = int(s)
int automatically converts to base 10 but you could also use int(s,2) to convert it to base 2. You'd then get the base 10 representation of your base2 number. So int("110",2) would be 6 and 6<<2 would be 24 or 6>>2 would be 1.
Also in terms of making the stringyfication easier yon can use
s = "".join(map(str, board))