Operadores bit a bit
AND, OR, XOR, NOT e deslocamentos.
Operadores bit a bit é uma aula grátis de Zig Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Zig Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Zig Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Down to the Bits
Every integer is really a row of bits. Bitwise operators let you work on those individual ones and zeros directly, with no rounding or math in between.
Bitwise AND
The & operator keeps a bit only where both inputs have a 1. It is the classic tool for masking off the bits you do not want.
const masked = value & 0x0F;Bitwise OR
The | operator sets a bit where either input has a 1. Use it to turn specific flags on without disturbing the rest.
const withFlag = value | 0b0010;Bitwise XOR
The ^ operator sets a bit only where the inputs differ. XOR is handy for toggling a flag or comparing two patterns.
const toggled = value ^ 0b0001;Bitwise NOT
The unary ~ operator flips every bit at once: each 1 becomes 0 and each 0 becomes 1. It builds the complement of a value.
const inverted = ~flags;Shifting Left
The << operator slides bits toward the high end, filling in zeros. Each shift left by one doubles an unsigned value.
const doubled = x << 1;Shifting Right
The >> operator moves bits toward the low end. For an unsigned integer, each right shift by one halves the value.
const halved = x >> 1;Masking Reads Bits
Combine AND with a mask to test whether a bit is set. If value & mask is nonzero, that flag is on; if zero, it is off.
const isSet = (value & mask) != 0;Setting and Clearing
OR a mask to set bits, and AND with an inverted mask to clear them. Together they give you precise control over each flag.
const cleared = value & ~mask;Shift Amounts Are Checked
Zig will not let you shift by more than the type holds. A shift amount out of range is a defined error, not silent garbage.
Why Bits Matter
Bit operations power flags, permissions, hardware registers, and compact data. They let you store many true-or-false answers in a single number.
Quick Check
You want a bit set only when the two inputs differ. Which operator?
Recap: Master of Bits
You now use &, |, ^, ~, plus the shifts << and >>, to read, set, clear, and move bits with full precision. ⚙️
Perguntas Frequentes
A aula “Operadores bit a bit” é grátis?
Sim — o texto completo de “Operadores bit a bit” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Zig Academy, atualize para CoddyKit PRO. O curso de Zig Academy inclui 4 aulas no total.
O que vou aprender em “Operadores bit a bit”?
AND, OR, XOR, NOT e deslocamentos. Você pratica Zig Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Zig Academy?
Nenhuma experiência prévia é necessária. Zig Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Operadores bit a bit”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Zig Academy?
Sim. Cada aula de Zig Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Operadores aritméticos e de comparação
- Overflow: + versus +% versus +|
- Operadores bit a bit
- Lógica booleana e curto-circuito