riscemu.types.int32 module

class riscemu.types.int32.Int32(val: Union[int, c_int, c_uint, Int32, bytes, bytearray, bool] = 0)

Bases: object

This class implements 32bit signed integers (see UInt32 for unsigned integers)

It implements basically all mathematical dunder magic methods (__add__, __sub__, etc.)

You can use it just like you would any other integer, just be careful when passing it to functions which actually expect an integer and not a Int32.

__init__(val: Union[int, c_int, c_uint, Int32, bytes, bytearray, bool] = 0)
property value: int

The value represented by this Integer :return:

unsigned() UInt32

Convert to an unsigned representation. See :class:Uint32 :return:

to_bytes(bytes: int = 4) bytearray

Convert to a bytearray of length :param:bytes

Parameters:

bytes – The length of the bytearray

Returns:

A little-endian representation of the contained integer

signed() Int32

Convert to a signed representation. See :class:Int32 :return:

property unsigned_value

Return the value interpreted as an unsigned integer :return:

shift_right_logical(ammount: Union[Int32, int]) Int32

This function implements logical right shifts, meaning that the sign bit is shifted as well.

This is equivalent to (self.value % 0x100000000) >> ammount

Parameters:

ammount – Number of positions to shift

Returns:

A new Int32 object representing the shifted value (keeps the signed-ness of the source)

classmethod sign_extend(data: Union[bytes, bytearray, int], bits: int)

Create an instance of Int32 by sign extending :param:bits bits from :param:data to 32 bits

Parameters:
  • data – The source data

  • bits – The number of bits in the source data

Returns:

An instance of Int32, holding the sign-extended value

class riscemu.types.int32.UInt32(val: Union[int, c_int, c_uint, Int32, bytes, bytearray, bool] = 0)

Bases: Int32

An unsigned version of :class:Int32.

unsigned() UInt32

Return a new instance representing the same bytes, but signed :return:

property unsigned_value: int

Return the value interpreted as an unsigned integer :return:

shift_right_logical(ammount: Union[Int32, int]) UInt32

see Int32.shift_right_logical

Parameters:

ammount – Number of positions to shift

Returns:

A new Int32 object representing the shifted value (keeps the signed-ness of the source)