Skip to content
Learn Netverks

Lesson

Step 28/36 78% through track

bit-operations

Bit operations

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Bit operations: the syntax, patterns, and safety habits you need before advancing in C.

Teams still ship Bit operations in C codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Bit operations in contexts like: Hardware registers, protocol packing, and checksum implementations.

Write C in main.c with int main(), click Run on server—the dev runner compiles with cc/gcc -std=c11 and runs the binary; read stderr for compile and linker errors (LEARNING_RUNNER_ENABLED=true).

When pointers, structs, and basic control flow from intermediate lessons are familiar.

Bitwise operators manipulate individual bits—essential for flags, embedded registers, codecs, and low-level protocols.

Operators

  • & — AND
  • | — OR
  • ^ — XOR
  • <<, >> — shifts
  • ~ — NOT

Mask example

#define FLAG_READY (1u << 0)
unsigned flags = FLAG_READY;
if (flags & FLAG_READY) { /* set */ }

Use unsigned types for portable shift behavior. Shifting past width is UB.

Important interview questions and answers

  1. Q: Set bit n?
    A: x |= (1u << n)
  2. Q: Clear bit n?
    A: x &= ~(1u << n)
  3. Q: Logical vs arithmetic shift?
    A: Right shift of signed negative values is implementation-defined in C—prefer unsigned for portable bit tricks.

Self-check

  1. What is 5 & 3?
  2. How do you test if bit 2 is set?

Interview prep

Set bit n?

flags |= (1u << n); — use unsigned types for portable shift behavior.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Mask idiom?
  • Endianness?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump