-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtestSHIFTbits.py
More file actions
45 lines (33 loc) · 815 Bytes
/
Copy pathtestSHIFTbits.py
File metadata and controls
45 lines (33 loc) · 815 Bytes
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
#!/usr/bin/env python
# Author Dario Clavijo 2018
def SRA(val, n):
s = val & 0x80000000
for _ in range(0, n):
val >>= 1
val |= s
return val
def SLL(val, n):
s = val & 0x80000000
for _ in range(0, n):
val <<= 1
return val & 0xFFFFFFFF
def SRL(val, n):
s = val & 0x80000000
for _ in range(0, n):
val >>= 1
return val & 0xFFFFFFFF
def bitrepr32(val):
return bin(val).replace("0b", "").zfill(32)
def test(a, n):
print("BIT:", bitrepr32(a))
print("SRA:", bitrepr32(SRA(a, n)), n)
print("SRL:", bitrepr32(SRL(a, n)), n)
print("SLL:", bitrepr32(SLL(a, n)), n)
n = 5
a = 0b11111111111111110000000000000000
test(a, 5)
n = 5
a = 0b00000000000000001111111111111111
test(a, 5)
a = 0b00000000111111111111111100000000
test(a, 5)