## Why is the maximum value of an unsigned n-bit integer 2ⁿ-1 and not 2ⁿ?

The -1 is because integers start at 0, but our counting starts at 1. So, 2^32-1 is the maximum value for a 32-bit unsigned integer (32 binary digits). 2^32 is the number of possible values. To simplify why, look at decimal. 10^2-1 is the maximum value of a 2-digit decimal number (99). Because our intuitive … Read more

## Hex string to signed int in Python 3.2?

In n-bit two’s complement, bits have value: bit 0 = 20 bit 1 = 21 bit n-2 = 2n-2 bit n-1 = -2n-1 But bit n-1 has value 2n-1 when unsigned, so the number is 2n too high. Subtract 2n if bit n-1 is set: >>> def twos_complement(hexstr,bits): … value = int(hexstr,16) … if value … Read more

## Simulate int variables with byte or short

Christmas special answer. Fully tested using Unit tests, but as yet only on Java SE. Needs some work for instantiating the backing array. Some code can still be optimized by in-lining the left hand operand. Note that this code uses *= – assignment of the answer to the first variable – rather than * as … Read more

## 128 bit integer on cuda?

For best performance, one would want to map the 128-bit type on top of a suitable CUDA vector type, such as uint4, and implement the functionality using PTX inline assembly. The addition would look something like this: typedef uint4 my_uint128_t; __device__ my_uint128_t add_uint128 (my_uint128_t addend, my_uint128_t augend) { my_uint128_t res; asm (“add.cc.u32 %0, %4, %8;\n\t” … Read more

## Signed versus Unsigned Integers

Unsigned can hold a larger positive value and no negative value. Yes. Unsigned uses the leading bit as a part of the value, while the signed version uses the left-most-bit to identify if the number is positive or negative. There are different ways of representing signed integers. The easiest to visualise is to use the … Read more

## What is the maximum value for an int32?

It’s 2,147,483,647. Easiest way to memorize it is via a tattoo.