Python doesn't have any built-in support for 8 or 16-bit integers. NumPy, on the other hand, does support the sizes you're looking for:
import numpy as np
print(np.uint8(22))
Answer from BrokenBenchmark on Stack OverflowPython doesn't have any built-in support for 8 or 16-bit integers. NumPy, on the other hand, does support the sizes you're looking for:
import numpy as np
print(np.uint8(22))
Afaik python chooses the type according to the size of the number and there is no way of specifying which type of int you want python to use. If you are concerned about the waste of memory, python may not be the correct choice in the first place.
python - what is the difference between int16 and in32 dtype - Stack Overflow
python 3.x - How to convert int32 numpy array into int16 numpy array? - Stack Overflow
c - Convert python int into int16_t type - Stack Overflow
What are some real world applications of having int8 , int16, int 32 etc ?
SOLVED: 3 Solutions:
-
using Numpy : np.uint16()
-
Using CTypes : ctypes.c_uint16()
-
Using Bitwise : & 0xFFFF
Hi, I'm trying to convert this code to Python from Go / C. It involves declaring a UInt16 variable and run a bit shift operation. However cant seem to create a variable with this specific type. Need some advise here.
Go Code:
package main
import "fmt"
func main() {
var dx uint16 = 38629
var dy uint16 = dx << 8
fmt.Println(dy) //58624 -> Correct value
}
Python Code:
dx = 38629 dy = (dx << 8) print(dy) # 9889024 -> not the expected value print(type(dx)) # <class 'int'> print(type(dy)) # <class 'int'>
I cant seem to figure out a way to cast or similar function to get this into an Unsigned Int 16.\
Please help.
It is the number of bits used to represent each integer, as you say, but consider what happens when you try to print the 10 int16 words as int32 words. Each int32 word will be built from two int16 words. Thus there will be only 5 int32words as you see.
Further, the byte order in 32 bit integers is so that the first 16 bit word will be the LSB bits. Thus looking at the hex representation of your two first words. In 16 bits they are: 0x0000 = 0 and 0x0001 = 1. When the same bits are used in a 32 bit word, you get 0x0001 0000 = 65536 due to this somewhat unintuitive bit order.
I hope this helps your understanding of the concepts.
Ref: Array types in Numpy and as per the ndarray.view doc
a.view(dtype=some_dtype) constructs a view of the array’s memory with a different data-type. This can cause a reinterpretation of the bytes of memory.
Consider
b = np.arange(10, dtype = 'int16')
It generates an evenly spaced array from 0 to 9.[0 1 2 3 4 5 6 7 8 9]
1.1 Viewing this array asint32merges the array by(32/16) = 2.
Also, trying to do this operation on an odd length array will result in error.
The output is[ 65536 196610 327684 458758 589832]
Explanation:[ 65536 * 1 + 0, 65536 * 3 + 2, 65536 * 5 + 4, 65536 * 7 + 6, 65536 * 9 + 8]Consider
b = np.arange(10, dtype = 'int32')
It is equalivalent tonp.arange(10)which simply creates an evenly spaced array from 0 to 9.
2.1 Viewing this data asint16splits each element in the data into(32/16) = 2parts.
Therefore,a = b.view(dtype = 'int16')is[0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0]
2.2 Viewing this data asint8splits each element in the data into(32/8) = 4parts.
Therefore,a = b.view(dtype = 'int8')is[0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0 6 0 0 0 7 0 0 0 8 0 0 0 9 0 0 0]
You could discard the bottom 16 bits:
n=(array_int32>>16).astype(np.int16)
which will give you this:
array([ 485, 1054, 2531], dtype=int16
The numbers in your array_int32 are too large to be represented with 16 bits (a signed integer value with 16 bits can only represent a maximum value of 2^16-1=32767).
Apparently, numpy just sets the resulting numbers to zero in this case.
This behavior can be modified by changing the optional casting argument of astype The documentation states
Starting in NumPy 1.9, astype method now returns an error if the string dtype to cast to is not long enough in ‘safe’ casting mode to hold the max value of integer/float array that is being casted. Previously the casting was allowed even if the result was truncated.
So, an additional requirement casting='safe' will result in a TypeError, as the conversion from 32 (or 64) bits downto 16, as the maximum value of original type is too large for the new type, e.g.
import numpy as np
array_int32 = np.array([31784960, 69074944, 165871616])
array_int16 = array_int32.astype(np.int16, casting='safe')
results in
TypeError: Cannot cast array from dtype('int64') to dtype('int16') according to the rule 'safe'