Sistema decimal é o sistema numérico mais utilizado. Mas o computador entende apenas binário.
Os sistemas de números binários, octais e hexadecimais estão intimamente relacionados e podemos exigir a conversão de decimal nesses sistemas.
O sistema decimal é a base 10 (dez símbolos, 0-9, são usados para representar um número) e, da mesma forma:
Um número com o prefixo '0b' é considerado binário, '0o' é considerado octal e '0x' como hexadecimal.
Por exemplo:
112
0b1110000
0o160
0x70
Vamos definir um número decimal para executarmos conversões
decimal = 8
print(f'Versão decimal: {decimal}')
print(f'Versão binária: {bin(decimal)}')
print(f'Versão octal: {oct(decimal)}')
print(f'Versão hexadecimal: {hex(decimal)}')
Versão decimal: 8 Versão binária: 0b1000 Versão octal: 0o10 Versão hexadecimal: 0x8
Vamos novamente definir um número decimal e fazer conversões
dec = 255
print(f'Decimal: {dec:.2f}')
print(f'Hexadecimal: {dec:x}')
print(f'Binário: {dec:b}')
print(f'Octal: {dec:o}')
print(f'Científico: {dec:e}')
Decimal: 255.00 Hexadecimal: ff Binário: 11111111 Octal: 377 Científico: 2.550000e+02
Vejamos informações e objetivos relacionados à manipulação de bits em Python (manipulação binária).
Algumas tarefas incluem:
int('00100001', 2)
33
print(f'{int("11111111", 2):x}')
ff
print(f'{int("0110110110", 2):x}')
1b6
print(f'{int("01111111", 2):x}')
7f
chr(int('111011', 2))
';'
chr(int('1110110', 2))
'v'
chr(int('1110111', 2))
'w'
chr(int('01111110', 2))
'~'
chr(int('111111', 2))
'?'
int('11111110', 2)
254
chr(int('11111110', 2))
'þ'
ord('þ')
254
ord('p')
112
chr(int('1110000', 2))
'p'
1 << 0
1
1 << 1
2
1 << 2
4
1 << 3
8
1 << 4
16
1 << 8
256
int('0xff', 16)
255
int('0x10', 16)
16
int('0o160', 8)
112
int('0o037', 8)
31
def bin(s):
return str(s) if s<=1 else bin(s>>1) + str(s&1)
bin(3)
'11'
bin(70)
'1000110'
bin(255)
'11111111'
from prettytable import PrettyTable
tabela = PrettyTable()
tabela.field_names = ["Decimal", "Hexadecimal", "Octal", "Binário"]
for n in range(128):
tabela.add_row([n, hex(n), oct(n), bin(n)])
print(tabela)
+---------+-------------+-------+---------+ | Decimal | Hexadecimal | Octal | Binário | +---------+-------------+-------+---------+ | 0 | 0x0 | 0o0 | 0 | | 1 | 0x1 | 0o1 | 1 | | 2 | 0x2 | 0o2 | 10 | | 3 | 0x3 | 0o3 | 11 | | 4 | 0x4 | 0o4 | 100 | | 5 | 0x5 | 0o5 | 101 | | 6 | 0x6 | 0o6 | 110 | | 7 | 0x7 | 0o7 | 111 | | 8 | 0x8 | 0o10 | 1000 | | 9 | 0x9 | 0o11 | 1001 | | 10 | 0xa | 0o12 | 1010 | | 11 | 0xb | 0o13 | 1011 | | 12 | 0xc | 0o14 | 1100 | | 13 | 0xd | 0o15 | 1101 | | 14 | 0xe | 0o16 | 1110 | | 15 | 0xf | 0o17 | 1111 | | 16 | 0x10 | 0o20 | 10000 | | 17 | 0x11 | 0o21 | 10001 | | 18 | 0x12 | 0o22 | 10010 | | 19 | 0x13 | 0o23 | 10011 | | 20 | 0x14 | 0o24 | 10100 | | 21 | 0x15 | 0o25 | 10101 | | 22 | 0x16 | 0o26 | 10110 | | 23 | 0x17 | 0o27 | 10111 | | 24 | 0x18 | 0o30 | 11000 | | 25 | 0x19 | 0o31 | 11001 | | 26 | 0x1a | 0o32 | 11010 | | 27 | 0x1b | 0o33 | 11011 | | 28 | 0x1c | 0o34 | 11100 | | 29 | 0x1d | 0o35 | 11101 | | 30 | 0x1e | 0o36 | 11110 | | 31 | 0x1f | 0o37 | 11111 | | 32 | 0x20 | 0o40 | 100000 | | 33 | 0x21 | 0o41 | 100001 | | 34 | 0x22 | 0o42 | 100010 | | 35 | 0x23 | 0o43 | 100011 | | 36 | 0x24 | 0o44 | 100100 | | 37 | 0x25 | 0o45 | 100101 | | 38 | 0x26 | 0o46 | 100110 | | 39 | 0x27 | 0o47 | 100111 | | 40 | 0x28 | 0o50 | 101000 | | 41 | 0x29 | 0o51 | 101001 | | 42 | 0x2a | 0o52 | 101010 | | 43 | 0x2b | 0o53 | 101011 | | 44 | 0x2c | 0o54 | 101100 | | 45 | 0x2d | 0o55 | 101101 | | 46 | 0x2e | 0o56 | 101110 | | 47 | 0x2f | 0o57 | 101111 | | 48 | 0x30 | 0o60 | 110000 | | 49 | 0x31 | 0o61 | 110001 | | 50 | 0x32 | 0o62 | 110010 | | 51 | 0x33 | 0o63 | 110011 | | 52 | 0x34 | 0o64 | 110100 | | 53 | 0x35 | 0o65 | 110101 | | 54 | 0x36 | 0o66 | 110110 | | 55 | 0x37 | 0o67 | 110111 | | 56 | 0x38 | 0o70 | 111000 | | 57 | 0x39 | 0o71 | 111001 | | 58 | 0x3a | 0o72 | 111010 | | 59 | 0x3b | 0o73 | 111011 | | 60 | 0x3c | 0o74 | 111100 | | 61 | 0x3d | 0o75 | 111101 | | 62 | 0x3e | 0o76 | 111110 | | 63 | 0x3f | 0o77 | 111111 | | 64 | 0x40 | 0o100 | 1000000 | | 65 | 0x41 | 0o101 | 1000001 | | 66 | 0x42 | 0o102 | 1000010 | | 67 | 0x43 | 0o103 | 1000011 | | 68 | 0x44 | 0o104 | 1000100 | | 69 | 0x45 | 0o105 | 1000101 | | 70 | 0x46 | 0o106 | 1000110 | | 71 | 0x47 | 0o107 | 1000111 | | 72 | 0x48 | 0o110 | 1001000 | | 73 | 0x49 | 0o111 | 1001001 | | 74 | 0x4a | 0o112 | 1001010 | | 75 | 0x4b | 0o113 | 1001011 | | 76 | 0x4c | 0o114 | 1001100 | | 77 | 0x4d | 0o115 | 1001101 | | 78 | 0x4e | 0o116 | 1001110 | | 79 | 0x4f | 0o117 | 1001111 | | 80 | 0x50 | 0o120 | 1010000 | | 81 | 0x51 | 0o121 | 1010001 | | 82 | 0x52 | 0o122 | 1010010 | | 83 | 0x53 | 0o123 | 1010011 | | 84 | 0x54 | 0o124 | 1010100 | | 85 | 0x55 | 0o125 | 1010101 | | 86 | 0x56 | 0o126 | 1010110 | | 87 | 0x57 | 0o127 | 1010111 | | 88 | 0x58 | 0o130 | 1011000 | | 89 | 0x59 | 0o131 | 1011001 | | 90 | 0x5a | 0o132 | 1011010 | | 91 | 0x5b | 0o133 | 1011011 | | 92 | 0x5c | 0o134 | 1011100 | | 93 | 0x5d | 0o135 | 1011101 | | 94 | 0x5e | 0o136 | 1011110 | | 95 | 0x5f | 0o137 | 1011111 | | 96 | 0x60 | 0o140 | 1100000 | | 97 | 0x61 | 0o141 | 1100001 | | 98 | 0x62 | 0o142 | 1100010 | | 99 | 0x63 | 0o143 | 1100011 | | 100 | 0x64 | 0o144 | 1100100 | | 101 | 0x65 | 0o145 | 1100101 | | 102 | 0x66 | 0o146 | 1100110 | | 103 | 0x67 | 0o147 | 1100111 | | 104 | 0x68 | 0o150 | 1101000 | | 105 | 0x69 | 0o151 | 1101001 | | 106 | 0x6a | 0o152 | 1101010 | | 107 | 0x6b | 0o153 | 1101011 | | 108 | 0x6c | 0o154 | 1101100 | | 109 | 0x6d | 0o155 | 1101101 | | 110 | 0x6e | 0o156 | 1101110 | | 111 | 0x6f | 0o157 | 1101111 | | 112 | 0x70 | 0o160 | 1110000 | | 113 | 0x71 | 0o161 | 1110001 | | 114 | 0x72 | 0o162 | 1110010 | | 115 | 0x73 | 0o163 | 1110011 | | 116 | 0x74 | 0o164 | 1110100 | | 117 | 0x75 | 0o165 | 1110101 | | 118 | 0x76 | 0o166 | 1110110 | | 119 | 0x77 | 0o167 | 1110111 | | 120 | 0x78 | 0o170 | 1111000 | | 121 | 0x79 | 0o171 | 1111001 | | 122 | 0x7a | 0o172 | 1111010 | | 123 | 0x7b | 0o173 | 1111011 | | 124 | 0x7c | 0o174 | 1111100 | | 125 | 0x7d | 0o175 | 1111101 | | 126 | 0x7e | 0o176 | 1111110 | | 127 | 0x7f | 0o177 | 1111111 | +---------+-------------+-------+---------+