Binary
Binary
A number system composed of 0s and 1s. The basic language of computers. A binary file is executable code.
Binary
A number system composed of 0s and 1s. The basic language of computers. A binary file is executable code.
Binary is a number system that represents all information using only two digits, 0 and 1. It is the most fundamental language of computers, and all digital data is ultimately stored and processed as binary numbers. It is physically implemented as the ON (1) and OFF (0) states of electrical signals, serving as the link between hardware and software.
Each digit of a binary number is called a bit, and 8 bits make up 1 byte. For example, the character 'A' is 65 in ASCII code, which is 01000001 in binary. In this way, all digital data, including text, images, video, and audio, is encoded as combinations of 0s and 1s.
In programming, binary operations mean bit-level manipulation, and include AND(&), OR(|), XOR(^), NOT(~), and shift (<<, >>) operations. Bitwise operations are faster than ordinary arithmetic and are essential in flag management, permission systems, encryption, and compression algorithms.
A binary file is a file made up of binary data rather than text. Executables (.exe), images (.jpg, .png), videos (.mp4), and compiled programs are binary files. In contrast, text files such as source code, HTML, and JSON consist of human-readable characters.
# 1. 10진수를 2진수로 변환
decimal = 42
binary_str = bin(decimal) # '0b101010'
binary_clean = bin(decimal)[2:] # '101010'
print(f"10진수 {decimal} → 2진수 {binary_clean}")
# 2. 2진수를 10진수로 변환
binary = "101010"
decimal_back = int(binary, 2) # 42
print(f"2진수 {binary} → 10진수 {decimal_back}")
# 3. 비트 연산 (Bitwise Operations)
a = 0b1100 # 12
b = 0b1010 # 10
print(f"AND: {bin(a & b)} = {a & b}") # 0b1000 = 8
print(f"OR: {bin(a | b)} = {a | b}") # 0b1110 = 14
print(f"XOR: {bin(a ^ b)} = {a ^ b}") # 0b0110 = 6
print(f"NOT: {bin(~a & 0xFF)} (8비트)") # 0b11110011
# 4. 비트 시프트 연산
x = 1
print(f"좌측 시프트: {x} << 4 = {x << 4}") # 16 (2의 4승)
print(f"우측 시프트: 16 >> 2 = {16 >> 2}") # 4 (16/4)
# 5. 실무 활용: 권한 플래그 시스템
READ = 0b100 # 4
WRITE = 0b010 # 2
EXEC = 0b001 # 1
user_perm = READ | WRITE # 0b110 = 6 (읽기+쓰기)
if user_perm & READ:
print("읽기 권한 있음")
if user_perm & EXEC:
print("실행 권한 있음")
else:
print("실행 권한 없음")
# 6. 바이너리 데이터 다루기
data = bytes([0x48, 0x65, 0x6C, 0x6C, 0x6F]) # "Hello"
print(f"바이트: {data}, 문자열: {data.decode('utf-8')}")
"I think we should manage these settings as bit flags. Eight options can be stored in 1 byte and checked quickly with bitwise operations, which is efficient for both memory and performance."
"Computers use binary because the two states of an electrical signal, ON and OFF, can be reliably distinguished. Negative numbers are handled with two's complement representation, and floating-point numbers are represented with the IEEE 754 standard."
"You used a left shift here instead of multiplying by 2. Modern compilers optimize this automatically, so for readability it's better to use plain multiplication. In bitmasking logic, though, the intent is clear, so a shift operation is appropriate."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Right shifts on negative numbers behave differently depending on the language. Distinguish between arithmetic shifts (sign preserved) and logical shifts (zero-filled), and use unsigned types explicitly.
Multibyte data has a different byte order in little-endian (Intel) and big-endian (network). Always check endianness when parsing network communication or file formats.
Bitwise operators have lower precedence than comparison operators. `if (flags & MASK == MASK)` does not behave as expected, so use parentheses like `if ((flags & MASK) == MASK)`.