Bits and binary · Bits e binário
Bits: the smallest piece of data
- A bit is one digit that is either
0or1. - Computers store everything — numbers, text, images — as bits.
- One bit alone is small, so we group many bits together.
Bits: a menor parte de dados
- Um bit é um único dígito que é ou
0ou1. - Os computadores armazenam tudo — números, texto, imagens — como bits.
- Um bit sozinho é pequeno, então agrupamos muitos bits juntos.
Why computers use binary
- A computer chip has tiny switches. Each switch is on or off.
on=1andoff=0. So two states fit a chip perfectly.- Counting with only
0and1is called binary (base 2).
Por que os computadores usam binário
- Um chip de computador tem interruptores minúsculos. Cada interruptor está ligado ou desligado.
on=1eoff=0. Então dois estados cabem perfeitamente num chip.- Contar usando apenas
0e1é chamado binário (base 2).
How binary counts
- Our normal numbers use base 10 (digits
0–9). - Binary uses base 2. Each place is worth twice the place on its right.
- The places are
8 4 2 1. So1101means8 + 4 + 0 + 1 = 13.
Como o binário conta
- Nossos números normais usam base 10 (dígitos
0–9). - Binário usa base 2. Cada posição vale duas vezes a posição à sua direita.
- As posições são
8 4 2 1. Então1101significa8 + 4 + 0 + 1 = 13.
place: 8 4 2 1
bits: 1 1 0 1 -> 8 + 4 + 0 + 1 = 13
Python can convert for you
bin(13)gives the string'0b1101'. The0bjust means "this is binary".int("1101", 2)reads"1101"as base 2 and gives13.- These two functions go in opposite directions.
Python pode converter por você
bin(13)devolve a string'0b1101'. O0bapenas significa "isto é binário".int("1101", 2)lê"1101"como base 2 e dá13.- Estas duas funções funcionam em direções opostas.
print(bin(13)) # decimal -> binary
print(int("1101", 2)) # binary -> decimal
Fixed bits and overflow
- A computer keeps a number in a fixed number of bits.
- With 4 bits you can store
0to15— that is2**4 = 16different values. - If a result is too big, it wraps around back to
0. This is overflow.
Bits fixos e transbordamento
- Um computador mantém um número num número fixo de bits.
- Com 4 bits você pode armazenar
0a15— isso é2**4 = 16valores diferentes. - Se um resultado for muito grande, ele volta para
0. Isso é transbordamento.
print(2 ** 4) # how many values 4 bits hold = 16
print((15 + 3) % 16) # 18 is too big, so it wraps to 2
Common mistakes
nbits store2**ndifferent values, from0to2**n - 1.- Binary is base 2: each place is worth twice the one to its right.
Erros comuns
nbits armazenam2**nvalores diferentes, de0a2**n - 1.- Binário é base 2: cada posição vale duas vezes a que está à sua direita.
Now you try
- Type your code below and press Run to see the output.
- Press Check answer to test it.
Agora você tenta
- Digite seu código abaixo e pressione Executar para ver a saída.
- Pressione Check answer para testá-lo.
Binary counts in powers of 2 · Binário conta em potências de 2
Each bit is a power of two; lit bits add up to the value. · Cada bit é uma potência de dois; bits acesos somam-se ao valor.
Use bin(...) to print the binary form of 20. The output should look like 0b10100. · Use bin(...) para imprimir a forma binária de 20. A saída deve parecer 0b10100.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
The binary string "110" is a number. Use int(s, 2) to print it as a normal (decimal) number. · A string binária "110" é um número. Use int(s, 2) para imprimi-lo como um número normal (decimal).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
A counter has 5 bits, so it holds 2**5 = 32 values. Store 2**5 in size. Then store (30 + 5) % size in result to show how the value wraps around (overflow). · Um contador tem 5 bits, então segura 2**5 = 32 valores. Armazene 2**5 em size. Depois armazene (30 + 5) % size em result para mostrar como o valor volta ao início (overflow).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.