-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumeric_systems.py
68 lines (57 loc) · 1.97 KB
/
numeric_systems.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#By Filipe Chagas
#Mar 2022
from typing import *
def natural_to_binary(x: int, n: int) -> List[bool]:
"""Returns x in the binary system.
:param x: Natural (including zero) to convert.
:type x: int
:param n: Number of bits.
:type n: int
:return: List of bits. Each bit is a bool. Most significant bit last.
:rtype: List[bool]
"""
assert n > 0
assert x >= 0
assert x < 2**n
return [bool((x//2**i)%2) for i in range(n)]
def binary_to_natural(x: List[bool]) -> int:
"""Returns x as a natural number (including zero).
:param x: List of bits. Most significant bit last.
:type x: List[bool]
:return: Natural number (including zero).
:rtype: int
"""
n = len(x)
return sum([(2**i)*int(x[i]) for i in range(n)])
def two_complement(x: List[bool]) -> List[bool]:
"""Returns the 2's complement for x.
:param x: List of bits. Most significant bit last.
:type x: List[bool]
:return: 2's complement of x. List of bits. Most significant bit last.
:rtype: List[bool]
"""
n = len(x)
one_complement = [not x_i for x_i in x]
return natural_to_binary(binary_to_natural(one_complement) + 1, n)
def integer_to_binary(x: int, n: int) -> List[bool]:
"""Returns x in the binary system using 2's complement notation.
:param x: Integer value to convert.
:type x: int
:param n: Number of bits.
:type n: int
:return: List of bits. Less significant bit fist. Sign bit last.
:rtype: List[bool]
"""
assert n > 0
assert x < 2**(n-1)
assert x >= -2**(n-1)
return natural_to_binary(x, n) if x >= 0 else two_complement(natural_to_binary(-x, n))
def binary_to_integer(x: List[bool]) -> int:
"""Returns x as an integer number.
:param x: List of bits. Less significant bit fist. Sign bit last.
:type x: List[bool]
:return: Integer number.
:rtype: int
"""
n = len(x)
return binary_to_natural(x) if x[n-1] == False else -binary_to_natural(two_complement(x))