It is a Python3 series. So in the following, ʻint` is a multiple precision integer.
We are introducing a bitboard to further promote the Reversi article I wrote earlier. Since 8x8 = 64 squares, it is expressed in 64 bits.
I wanted to randomly select one from the list of ʻint` and perform bit operation with the board, so I wrote the following code.
In [2]: array = [1, 2, 3]
In [3]: board = 0xfffffffffffffff
In [4]: np.random.choice(array) & board
Out[4]: 2
One is taken out from the list with numpy.random.choice
and bit operation is performed.
By the way, did you notice that the variable board
uses only 15 fs, that is, the range that can be represented by 60 bits?
Since the board is represented by up to 64 bits, try increasing f by one to make it 64 bits.
In [5]: array = [1, 2, 3]
In [6]: board = 0xffffffffffffffff
In [7]: np.random.choice(array) & board
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-cb36edc17d23> in <module>()
----> 1 np.random.choice(array) & board
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
It seems that an error will occur if the number of bits of board
is 64.
Let's take a look at the type of what appears in np.random.choice
.
In [36]: type(np.random.choice(array))
Out[36]: numpy.int64
ʻArray is a list of ʻint
, but it comes out as numpy.int64
!
It seems that numpy.int64
and 64-bit ʻint` cannot perform bit operations.
In [49]: np.int64(1) & board
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-49-3927f8b2d3d4> in <module>()
----> 1 np.int64(1) & board
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
By the way, when I checked it, it seems that it is not possible to calculate ** by limiting it to 64-bit ʻint`. what is that.
In [24]: board = 0b111111111111111111111111111111111111111111111111111111111111111 # 63bit
In [25]: np.random.choice(array) & board
Out[25]: 1
In [26]: board = 0b1111111111111111111111111111111111111111111111111111111111111111 # 64bit
In [27]: np.random.choice(array) & board---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-cb36edc17d23> in <module>()
----> 1 np.random.choice(array) & board
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
In [28]: board = 0b11111111111111111111111111111111111111111111111111111111111111111 # 65bit
In [29]: np.random.choice(array) & board
Out[29]: 3
Although casting
is somehow written, the operation result of 63bit is numpy.int64
and the operation result of 65bit is ʻint`, so I don't know what to cast the boundary 64bit to. Is that so?
Since numpy.int64
and 64-bit ʻint cannot be bitwise operated, it seems good to cast to
numpy.uint64`.
In [55]: np.uint64(np.random.choice(array)) & board
Out[55]: 1
Maybe even negative numbers.
In [24]: np.uint64(-1)
Out[24]: 18446744073709551615
Considering the original board type, I would like to cast it to ʻint`, but I am not worried about the bit operation of negative numbers. I don't really understand this for multiple-precision integers.
In the first place, the return type of np.random.choice
also changes depending on the contents.
In [13]: type(np.random.choice([1]))
Out[13]: numpy.int64
In [14]: type(np.random.choice([1<<63]))
Out[14]: numpy.uint64
In [15]: type(np.random.choice([1<<64]))
Out[15]: int
Too scary
Recommended Posts