# Bitwise calculator (AND, OR, XOR, NOT and shifts)

> Bitwise AND, OR, XOR, NOT, NAND, NOR, shifts and rotates on 8- to 64-bit integers, shown in decimal, hex and binary with a bit-by-bit grid.

직접 계산할 수 있는 페이지: https://www.calcopenly.com/ko/programming/bitwise-calculator
분야: 프로그래밍 및 기술 계산기

Bitwise operators work on each bit position separately: AND gives 1 only where both bits are 1, OR where either is, XOR where they differ, and NOT flips every bit. Shifts move the whole pattern left or right, filling with zeros, or with copies of the sign bit for an arithmetic right shift, and rotates carry the bits that fall off one end back in at the other. Every result is cut to the chosen width of 8, 16, 32 or 64 bits.

Programmers use it to build and test bit masks, flags, permissions and hardware registers. The default, 0xCA AND 0x5C in 8 bits, keeps only the bits the two share: 1100 1010 AND 0101 1100 = 0100 1000, which is 0x48 or 72. XOR of the same pair gives 0x96, which reads as 150 unsigned or −106 signed.

Negative inputs are stored in two's complement, so −1 in 16 bits is 0xFFFF.

## 입력

- **Operation** (선택 항목: A AND B, A OR B, A XOR B, A NAND B, A NOR B, A XNOR B, NOT A, Shift A left, Shift A right (logical, fills with 0), Shift A right (arithmetic, copies the sign bit), Rotate A left, Rotate A right)
- **A**: Decimal, or prefix with 0x, 0b or 0o. Negative numbers are stored in two's complement.
- **B**
- **Shift by (bits)**
- **너비** (선택 항목: 8-bit, 16-bit, 32-bit, 64-bit)
- **Read the result as** (선택 항목: Unsigned, Signed)

## 결과

- 결과 — 주요 결과
- Hex
- Binary
- Octal
- Same bits, other reading
- Bits set to 1

## 공식

$$
\begin{aligned} \text{unsigned} &= \sum_{i=0}^{w-1} b_i\,2^i \\ \text{signed} &= -b_{w-1}\,2^{w-1} + \sum_{i=0}^{w-2} b_i\,2^i\end{aligned}
$$

## 계산 예제

### 0xCA AND 0x5C (8-bit)

- Operation: A AND B
- A: 0xCA
- B: 0x5C
- 너비: 8-bit
- Read the result as: Unsigned
- **결과: 72**
- **Hex: 0x48**
- **Binary: 0100 1000**
- **Bits set to 1: 2**
- 검증 출처: Python 3.8: 0xCA & 0x5C = 72 = 0x48, bin(72).count('1') = 2

### XOR read as signed

- Operation: A XOR B
- A: 0xCA
- B: 0x5C
- 너비: 8-bit
- Read the result as: Signed
- **결과: -106**
- **Hex: 0x96**
- **Same bits, other reading: 150 as unsigned**
- 검증 출처: Python 3.8: 0xCA ^ 0x5C = 150; int.from_bytes(bytes([150]), 'big', signed=True) = -106

### NOT 0 in 64 bits (edge)

- Operation: NOT A
- A: 0
- 너비: 64-bit
- Read the result as: Unsigned
- **결과: 18446744073709551615**
- **Hex: 0xFFFFFFFFFFFFFFFF**
- **Same bits, other reading: -1 as signed**
- **Bits set to 1: 64**
- 검증 출처: Python 3.8: ~0 & (2**64 - 1) = 18446744073709551615

### Arithmetic shift right keeps the sign

- Operation: Shift A right (arithmetic, copies the sign bit)
- A: -128
- Shift by (bits): 2
- 너비: 8-bit
- Read the result as: Signed
- **결과: -32**
- **Hex: 0xE0**
- 검증 출처: Python 3.8: -128 >> 2 = -32 (Python's >> on ints is arithmetic); -32 & 0xFF = 0xE0

### Logical shift right fills with zeros

- Operation: Shift A right (logical, fills with 0)
- A: 0x80
- Shift by (bits): 2
- 너비: 8-bit
- Read the result as: Unsigned
- **결과: 32**
- **Hex: 0x20**
- 검증 출처: Python 3.8: 0x80 >> 2 = 32 on the unsigned value

### Rotate left wraps the top bit

- Operation: Rotate A left
- A: 0b1000_0001
- Shift by (bits): 1
- 너비: 8-bit
- Read the result as: Unsigned
- **결과: 3**
- **Binary: 0000 0011**
- 검증 출처: Python 3.8: ((0x81 << 1) | (0x81 >> 7)) & 0xFF = 3

## 자주 묻는 질문

### What does XOR do?

XOR (exclusive or) gives 1 where the two bits differ and 0 where they match, so 0xCA XOR 0x5C = 0x96. Two properties make it useful: x XOR x = 0, and applying the same key twice restores the original. That is why XOR toggles flags, computes parity and checksums, and appears in simple ciphers. C, Java, JavaScript and Python all write it as ^.

### What is the difference between a logical and an arithmetic right shift?

A logical right shift fills the vacated top bits with 0, while an arithmetic shift copies the sign bit so negative numbers stay negative. In 8 bits, 0x80 shifted right by 2 logically is 0x20 (32), but −128 shifted right by 2 arithmetically is 0xE0 (−32), which is −128 ÷ 4. Java and JavaScript write >>> for logical and >> for arithmetic; in C, >> on a negative signed value is implementation-defined.

### How does a bit mask work?

A mask is a number whose 1 bits pick the positions you care about. AND with the mask keeps those bits and clears the rest, OR sets them, XOR toggles them, and AND with NOT mask clears them. x AND 0x0F keeps the low 4 bits, and −1 AND 0x0F0F in 16 bits gives 0x0F0F = 3855, because −1 is all ones. To test bit n, check whether x AND (1 << n) is non-zero.

### What does a left shift do to a number?

Shifting left by n multiplies an unsigned value by 2^n as long as no 1 bits are pushed past the top: 0x0F << 4 is 0xF0 = 240. Bits that move beyond the width are lost, so in 8 bits 0xFF << 4 is also 0xF0, not 4,080. In C, shifting by the full width or more is undefined behavior, and x86 masks the shift count to 5 bits for 32-bit operands and 6 bits for 64-bit ones.

### What is popcount?

Popcount (population count, or Hamming weight) is the number of 1 bits in a value: 0x48 = 0100 1000 has 2, and NOT 0 in 64 bits has 64. x86 has a single POPCNT instruction for it, C++20 exposes it as std::popcount and Java as Integer.bitCount. It counts set flags and gives the Hamming distance between two values as popcount(a XOR b).

### “Bitwise calculator (AND, OR, XOR, NOT and shifts)”의 정확도는 어느 정도인가요?

정확도는 입력값과 계산 방법의 가정에 따라 달라집니다. 십진 연산은 유효숫자 50자리를 사용하지만, 추정값·수치해석 방법·원본 데이터의 정밀도는 더 낮을 수 있습니다. 표시값을 반올림해도 이러한 한계는 사라지지 않습니다. 독립적인 출처의 풀이와 대조한 계산 예시: 8. 예를 들어 “0xCA AND 0x5C (8-bit)”은 Python 3.8: 0xCA & 0x5C = 72 = 0x48, bin(72).count('1') = 2와 대조해 확인합니다.

### 이 계산 방법의 출처는 무엇인가요?

ISO/IEC 9899:2024 (C23) §6.5.7 bitwise shift operators and §6.5.10–6.5.12 bitwise AND, XOR, OR (draft N3096); Intel 64 and IA-32 Architectures Software Developer's Manual, Vol. 2 — SAL/SAR/SHL/SHR and ROL/ROR.

## 출처

- [ISO/IEC 9899:2024 (C23) §6.5.7 bitwise shift operators and §6.5.10–6.5.12 bitwise AND, XOR, OR (draft N3096)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf)
- [Intel 64 and IA-32 Architectures Software Developer's Manual, Vol. 2 — SAL/SAR/SHL/SHR and ROL/ROR](https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html)
