πŸ› οΈToolsShed

Bitwise Calculator

Perform bitwise operations with binary visualization.

Result

10

Binary representation (8-bit)
A00101010
B00011011
=00001010

Frequently Asked Questions

Code Implementation

# Bitwise operations in Python

a = 0b1100  # 12
b = 0b1010  # 10

# Basic bitwise operations
print(a & b)   # AND: 8  (0b1000)
print(a | b)   # OR:  14 (0b1110)
print(a ^ b)   # XOR: 6  (0b0110)
print(~a)      # NOT: -13 (inverts all bits)

# Bit shifts
print(a << 2)  # Left shift: 48 (0b110000)
print(a >> 1)  # Right shift: 6  (0b110)

# Practical: bitmask for permissions
READ    = 0b001  # 1
WRITE   = 0b010  # 2
EXECUTE = 0b100  # 4

perms = READ | WRITE  # 3
print(perms & READ)    # 1 β€” has read permission
print(perms & EXECUTE) # 0 β€” no execute permission

# Set/clear/toggle a bit
n = 0b10110
bit = 2
n |= (1 << bit)   # set bit 2
n &= ~(1 << bit)  # clear bit 2
n ^= (1 << bit)   # toggle bit 2

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.