☼
⌃
☰

Binary

Table of Contents

Base-2 number system using only 0 and 1. The native language of digital hardware — every value in a computer is ultimately represented as a sequence of bits. Relevant in security for encoding, protocol analysis, file format inspection, and understanding bitwise operations in exploit development.

Related: Hexadecimal Base64 CyberChef

Place Values

Each bit position represents a power of 2, right to left:

TEXT
Bit position:  7    6    5    4    3    2    1    0
Power of 2:   128   64   32   16    8    4    2    1
Binary:         1    0    1    0    1    1    0    1
Decimal:      128 +  0 + 32 +  0 +  8 +  4 +  0 +  1 = 173

Conversion

Binary → Decimal

Sum the place values for every bit that is 1:

TEXT
1010 1101
= 128 + 0 + 32 + 0 + 8 + 4 + 0 + 1
= 173

Decimal → Binary

Repeatedly divide by 2, read remainders bottom to top:

TEXT
173 ÷ 2 = 86 r 1   ↑ (LSB)
 86 ÷ 2 = 43 r 0   |
 43 ÷ 2 = 21 r 1   |
 21 ÷ 2 = 10 r 1   |
 10 ÷ 2 =  5 r 0   |
  5 ÷ 2 =  2 r 1   |
  2 ÷ 2 =  1 r 0   |
  1 ÷ 2 =  0 r 1   ↓ (MSB)
Result: 1010 1101

Binary ↔ Hexadecimal

Group bits into nibbles (4 bits) and convert each:

TEXT
Binary:  1010  1101
Hex:       A     D
Result: 0xAD

Hex to binary — reverse: each hex digit → 4 bits
0xAD → 1010 1101

Common shell conversions

SHELL
# Decimal to binary
python3 -c "print(bin(173))"          # 0b10101101
printf "%08b\n" 173                    # 10101101

# Binary to decimal
python3 -c "print(int('10101101', 2))" # 173

# Decimal to hex
python3 -c "print(hex(173))"          # 0xad
printf "%x\n" 173                     # ad

# Hex to decimal
python3 -c "print(int('ad', 16))"     # 173
printf "%d\n" 0xad                    # 173

Bits, Bytes, and Common Sizes

Unit Bits Bytes Notes
Bit 1 — Smallest unit (0 or 1)
Nibble 4 0.5 One hex digit
Byte 8 1 Values 0–255
Word 16 2 Platform-dependent
Dword 32 4 Common in x86
Qword 64 8 Common in x64
Kilobyte (KB) — 1,024 2¹⁰
Megabyte (MB) — 1,048,576 2²⁰

Bitwise Operations

Used in networking (subnet masks), file permissions, flags, and low-level security work.

Operator Symbol Effect
AND & 1 only where both bits are 1
OR =\ = 1 where either bit is 1
XOR ^ 1 where bits differ
NOT ~ Flip all bits
Left shift << Multiply by 2 per shift
Right shift >> Divide by 2 per shift
PYTHON
a = 0b1010   # 10
b = 0b1100   # 12

print(bin(a & b))   # 0b1000  (AND  → 8)
print(bin(a | b))   # 0b1110  (OR   → 14)
print(bin(a ^ b))   # 0b0110  (XOR  → 6)
print(bin(~a))      # -0b1011 (NOT  → -11 in two's complement)
print(bin(a << 1))  # 0b10100 (×2   → 20)
print(bin(a >> 1))  # 0b0101  (÷2   → 5)

Subnet masking (AND)

TEXT
IP:   192.168.  1. 10  →  11000000.10101000.00000001.00001010
Mask: 255.255.255.  0  →  11111111.11111111.11111111.00000000
AND:  192.168.  1.  0  →  11000000.10101000.00000001.00000000
                           ↑ network address

Security Relevance

File magic bytes (hex/binary signatures)

File types are identified by their first bytes, not their extension — relevant in file upload bypass and forensics:

TEXT
FF D8 FF        → JPEG
89 50 4E 47     → PNG  (‰PNG)
47 49 46 38     → GIF  (GIF8)
4D 5A           → PE/EXE  (MZ)
25 50 44 46     → PDF  (%PDF)
50 4B 03 04     → ZIP / docx / jar
7F 45 4C 46     → ELF (Linux executable)

XOR encoding

XOR is the simplest obfuscation — XOR with a key, XOR again with the same key to recover the original:

PYTHON
data = b"hello"
key  = 0x42

encrypted = bytes([b ^ key for b in data])
decrypted = bytes([b ^ key for b in encrypted])
# decrypted == data

Common in shellcode obfuscation, simple malware, and CTF encoding challenges.

Checking bits and flags

Many security-relevant values are bitfields — each bit represents an independent flag:

PYTHON
# Unix file permissions: 755 = 0b111 101 101
perms = 0o755
owner_exec = bool(perms & 0o100)   # True
world_write = bool(perms & 0o002)  # False

# TCP flags
SYN = 0x02
ACK = 0x10
flags = 0x12   # SYN-ACK
is_syn = bool(flags & SYN)   # True
is_ack = bool(flags & ACK)   # True

Tips

  • Hex is binary in shorthand — every two hex digits is one byte; shift between them mentally rather than converting to decimal
  • XOR is reversible with the same key — when you see XOR in malware deobfuscation, applying it again to the output undoes it
  • Magic bytes are more reliable than file extensions for file type identification — file and hexdump are faster than guessing
  • python3 -c "print(bin(n))" is the fastest interactive converter during CTF work