bitwise #36

Merged
defunkt merged 2 commits from bitwise into main 2025-11-09 00:11:19 +00:00
Owner
# Bitwise operations
5 band 3      # → 1  (0101 & 0011 = 0001)
5 bor 3       # → 7  (0101 | 0011 = 0111)
5 bxor 3      # → 6  (0101 ^ 0011 = 0110)
bnot 5        # → -6 (bitwise NOT)

# Bit shifting
5 << 2        # → 20 (shift left)
20 >> 2       # → 5  (shift right, preserves sign)
-1 >>> 1      # → 2147483647 (unsigned shift)

# Setting and checking file permissions (Unix-style)
read-perm = 4
write-perm = 2
exec-perm = 1

# Combine permissions
permissions = read-perm bor write-perm bor exec-perm  # → 7 (rwx)

# Check if readable
can-read = (permissions band read-perm) == read-perm  # → true

# Remove write permission
permissions = (permissions band (bnot write-perm))  # → 5 (r-x)

# Efficient bit manipulation
flags = 0
flags = flags bor (1 << 3)   # Set bit 3
flags = flags bor (1 << 7)   # Set bit 7
has-flag = (flags band (1 << 3)) != 0  # Check bit 3 → true

# Fast multiplication/division by powers of 2
result = 42 << 2   # → 168 (multiply by 4)
result = 168 >> 2  # → 42 (divide by 4)
``` # Bitwise operations 5 band 3 # → 1 (0101 & 0011 = 0001) 5 bor 3 # → 7 (0101 | 0011 = 0111) 5 bxor 3 # → 6 (0101 ^ 0011 = 0110) bnot 5 # → -6 (bitwise NOT) # Bit shifting 5 << 2 # → 20 (shift left) 20 >> 2 # → 5 (shift right, preserves sign) -1 >>> 1 # → 2147483647 (unsigned shift) # Setting and checking file permissions (Unix-style) read-perm = 4 write-perm = 2 exec-perm = 1 # Combine permissions permissions = read-perm bor write-perm bor exec-perm # → 7 (rwx) # Check if readable can-read = (permissions band read-perm) == read-perm # → true # Remove write permission permissions = (permissions band (bnot write-perm)) # → 5 (r-x) # Efficient bit manipulation flags = 0 flags = flags bor (1 << 3) # Set bit 3 flags = flags bor (1 << 7) # Set bit 7 has-flag = (flags band (1 << 3)) != 0 # Check bit 3 → true # Fast multiplication/division by powers of 2 result = 42 << 2 # → 168 (multiply by 4) result = 168 >> 2 # → 42 (divide by 4) ```
defunkt added 2 commits 2025-11-08 08:12:44 +00:00
defunkt force-pushed bitwise from cf09f51380 to 99a5aa5312 2025-11-09 00:11:14 +00:00 Compare
defunkt merged commit 4c794944ef into main 2025-11-09 00:11:19 +00:00
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: probablycorey/shrimp#36
No description provided.