-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
120 lines (94 loc) · 3.72 KB
/
Copy pathmain.py
File metadata and controls
120 lines (94 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
def demonstrate_bitwise_operations():
"""Demonstrate basic bitwise operations"""
a = 12 # 1100 in binary
b = 10 # 1010 in binary
print(f"a = {a} ({bin(a)})")
print(f"b = {b} ({bin(b)})")
print(f"a & b = {a & b} ({bin(a & b)}) # AND")
print(f"a | b = {a | b} ({bin(a | b)}) # OR")
print(f"a ^ b = {a ^ b} ({bin(a ^ b)}) # XOR")
print(f"~a = {~a} ({bin(~a & 0xFF)}) # NOT (8-bit)")
print(f"a << 2 = {a << 2} ({bin(a << 2)}) # Left shift")
print(f"a >> 2 = {a >> 2} ({bin(a >> 2)}) # Right shift")
def bit_manipulation_tricks():
"""Demonstrate common bit manipulation tricks"""
print("\n=== Bit Manipulation Tricks ===")
# Check if number is power of 2
num = 16
is_power_of_2 = (num & (num - 1)) == 0 and num != 0
print(f"{num} is power of 2: {is_power_of_2}")
# Set nth bit
number = 5 # 0101
n = 1
set_bit = number | (1 << n)
print(f"Set bit {n} in {number}: {set_bit} ({bin(set_bit)})")
# Clear nth bit
clear_bit = number & ~(1 << n)
print(f"Clear bit {n} in {number}: {clear_bit} ({bin(clear_bit)})")
# Toggle nth bit
toggle_bit = number ^ (1 << n)
print(f"Toggle bit {n} in {number}: {toggle_bit} ({bin(toggle_bit)})")
# Count set bits
count = bin(number).count('1')
print(f"Number of set bits in {number}: {count}")
def byte_manipulation():
"""Demonstrate byte-level operations"""
print("\n=== Byte Manipulation ===")
# Convert integer to bytes and back
num = 1234
byte_data = num.to_bytes(4, byteorder='big')
print(f"Integer {num} as bytes: {byte_data.hex()}")
reconstructed = int.from_bytes(byte_data, byteorder='big')
print(f"Bytes back to integer: {reconstructed}")
# Bit packing/unpacking
r, g, b = 255, 128, 64
packed = (r << 16) | (g << 8) | b
print(f"RGB({r}, {g}, {b}) packed: 0x{packed:06X}")
unpacked_r = (packed >> 16) & 0xFF
unpacked_g = (packed >> 8) & 0xFF
unpacked_b = packed & 0xFF
print(f"Unpacked: RGB({unpacked_r}, {unpacked_g}, {unpacked_b})")
def flags_example():
"""Demonstrate using bits as flags"""
print("\n=== Flag Operations ===")
# Define flags
READ = 1 << 0 # 001
WRITE = 1 << 1 # 010
EXECUTE = 1 << 2 # 100
# Set permissions
permissions = READ | WRITE
print(f"Initial permissions: {bin(permissions)}")
# Check if has permission
has_read = bool(permissions & READ)
has_execute = bool(permissions & EXECUTE)
print(f"Has read: {has_read}, Has execute: {has_execute}")
# Add execute permission
permissions |= EXECUTE
print(f"After adding execute: {bin(permissions)}")
# Remove write permission
permissions &= ~WRITE
print(f"After removing write: {bin(permissions)}")
def binary_string_operations():
"""Demonstrate binary string manipulations"""
print("\n=== Binary String Operations ===")
text = "Hello"
binary_text = ' '.join(format(ord(char), '08b') for char in text)
print(f"'{text}' in binary: {binary_text}")
# Convert back
binary_values = binary_text.split()
decoded = ''.join(chr(int(b, 2)) for b in binary_values)
print(f"Decoded back: '{decoded}'")
# XOR encryption/decryption
key = 42
encrypted = [ord(char) ^ key for char in text]
print(f"XOR encrypted with key {key}: {encrypted}")
decrypted = ''.join(chr(byte ^ key) for byte in encrypted)
print(f"Decrypted: '{decrypted}'")
if __name__ == "__main__":
print("=== Binary Manipulation Demonstrations ===\n")
demonstrate_bitwise_operations()
bit_manipulation_tricks()
byte_manipulation()
flags_example()
binary_string_operations()