MCPcopy Create free account
hub / github.com/bterwijn/memory_graph / twos_complement

Function twos_complement

src/bitwise_operators.py:6–9  ·  view source on GitHub ↗

Return the two's complement bit string of x in `bits` bits.

(x: int, bits: int)

Source from the content-addressed store, hash-verified

4 """ Dictionary subclass that we will configure to show binary representations. """
5
6def twos_complement(x: int, bits: int) -> str:
7 """Return the two's complement bit string of x in `bits` bits."""
8 mask = (1 << bits) - 1
9 return format(x & mask, f"0{bits}b")
10
11# configure memory_graph to show binary representations of values of type Bits
12mg.config.type_to_node[Bits] = lambda x : mg.Node_Table(x,

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected