5-min deep dive

Logic circuits in depth: De Morgan, NAND and bit masks

Once you know AND, OR, NOT and XOR, the next step is how to combine them. From a classic exam law to a calculation used every day in network settings, it’s all one connected thread.

Reading time: about 5 min

← Start with the 1-min version

In the game, this was light switches in a roomPlay “Light Up the Room”

In the gameIn real IT
Switch on / off1 and 0
Series wiringAND
Parallel wiringOR
Stair switches (top and bottom)XOR
Fridge door switchNOT
Switch-and-light tableTruth table

De Morgan’s laws

NOT (A AND B) = (NOT A) OR  (NOT B)
NOT (A OR  B) = (NOT A) AND (NOT B)

“It’s not true that I drink both tea and coffee” means the same as “I don’t drink tea, or I don’t drink coffee.” The way to remember it: when you push NOT inside, AND and OR swap. Check with a truth table and every row matches.

ABNOT (A AND B)(NOT A) OR (NOT B)
0011
0111
1011
1100

NAND alone can build anything

NAND is “AND with the result flipped,” so it’s 0 only when both inputs are 1. Feed the same signal into both NAND inputs and you get NOT. Put a NOT after a NAND and you get AND. NOT each input before feeding them into a NAND, and by De Morgan’s laws you get OR. If you can make NOT, AND and OR, you can make any circuit.

In other words, you can build complex circuits just by lining up many copies of one kind of part. The name of “NAND flash memory,” used in SSDs, comes from the way its storage cells are wired, which resembles a NAND circuit.

Pick out just the bits you need with AND

AND has this property: “AND with 0 gives 0; AND with 1 leaves the bit as is.” AND with a value where only the wanted part is 1 (a mask), and you pick out just the bits you need.

    1011 0110
AND 0000 1111   ← only the lower 4 bits are 1
=   0000 0110   ← only the lower 4 bits remain

This is used all the time in network settings. AND an IP address with its subnet mask, and you get the “network address” that IP address belongs to.

IP address        192.168.  1. 10
Subnet mask       255.255.255.  0
AND result        192.168.  1.  0   ← network address

255 is 1111 1111 in binary, so that part stays as is, and the 0 parts become 0. If two PCs have the same network address, they can talk directly without going through a router.

What XOR is used for

XOR has the property that “XOR the same thing twice and it disappears” (A XOR B XOR B = A). That’s used all over: the lower digit in addition, rebuilding data from RAID 5 parity, simple ciphers that XOR data with a key, and more.

Field note: the difference between “/24” and “/25”

The number of 1s in a subnet mask is written like “/24.” With /24 the lower 8 bits are free, so one network has 256 addresses (254 usable for devices). Make it /25 and the mask gains one more 1, halving it to 128. Cut a network too small where servers are going to be added, and you’ll run out of addresses later.

Practice questions

Four-choice questions in the style of Japan’s Fundamental Information Technology Engineer Examination. Tap below to see the answer.

Q1Which logical expression gives the same result as NOT (A AND B)?

  • A(NOT A) AND (NOT B)
  • B(NOT A) OR (NOT B)
  • CA OR B
  • DNOT (A OR B)
Show the answer and explanation

Answer: BBy De Morgan’s laws, pushing NOT inside swaps AND and OR. NOT (A AND B) = (NOT A) OR (NOT B).

Q2What is the result of ANDing the 8-bit data 1011 0110 with 0000 1111?

  • A0000 0110
  • B1011 1111
  • C1011 0000
  • D1011 1001
Show the answer and explanation

Answer: AAND gives 1 only when both bits are 1. ANDing with 0000 1111 makes the upper 4 bits 0, leaving only the lower 4 bits, 0110. B is the OR result and D the XOR result.

Q3If the IP address is 172.16.5.130 and the subnet mask is 255.255.255.0, what is the network address?

  • A172.16.0.0
  • B172.16.5.0
  • C172.16.5.128
  • D172.16.5.255
Show the answer and explanation

Answer: BAND the IP address and subnet mask bit by bit. The 255 parts stay as they are and the 0 parts become 0, giving 172.16.5.0.

Summary
  • De Morgan: pushing NOT inside swaps AND and OR
  • NAND alone can make NOT, AND and OR = it can build any circuit
  • An AND mask picks out the bits you need. AND an IP address with its subnet mask to get the network address