-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added binary shifts and twos complement functions to bit manipulation #4068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
19c0aee
ec0fe34
6911d3b
73dbc93
919af32
b7658fb
cbc0a39
b7750e1
b7ebf35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from binary_twos_complement import twos_complement | ||
|
||
|
||
def logical_left_shift(a: int, b: int) -> str: | ||
""" | ||
Take in 2 positive integers. | ||
'a' is the integer to be logically left shifted 'b' times. | ||
i.e. (a << b) | ||
Return the shifted binary representation. | ||
|
||
>>> logical_left_shift(0, 1) | ||
'0b00' | ||
>>> logical_left_shift(1, 1) | ||
'0b10' | ||
>>> logical_left_shift(1, 5) | ||
'0b100000' | ||
>>> logical_left_shift(17, 2) | ||
'0b1000100' | ||
>>> logical_left_shift(1983, 4) | ||
'0b111101111110000' | ||
>>> logical_left_shift(1, -1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: both inputs must be positive integers | ||
""" | ||
if a < 0 or b < 0: | ||
raise ValueError("both inputs must be positive integers") | ||
|
||
binary_a = str(bin(a)) | ||
binary_a += '0' * b | ||
return binary_a | ||
|
||
|
||
def logical_right_shift(a: int, b: int) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
""" | ||
Take in positive 2 integers. | ||
'a' is the integer to be logically right shifted 'b' times. | ||
i.e. (a >> b) | ||
Return the shifted binary representation. | ||
|
||
>>> logical_right_shift(0, 1) | ||
'0b0' | ||
>>> logical_right_shift(1, 1) | ||
'0b0' | ||
>>> logical_right_shift(1, 5) | ||
'0b0' | ||
>>> logical_right_shift(17, 2) | ||
'0b100' | ||
>>> logical_right_shift(1983, 4) | ||
'0b1111011' | ||
>>> logical_right_shift(1, -1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: both inputs must be positive integers | ||
""" | ||
if a < 0 or b < 0: | ||
raise ValueError("both inputs must be positive integers") | ||
|
||
binary_a = str(bin(a))[2:] | ||
if b >= len(binary_a): | ||
return '0b0' | ||
shifted_binary_a = binary_a[:len(binary_a) - b] | ||
return '0b' + shifted_binary_a | ||
|
||
|
||
def arithmetic_right_shift(a: int, b: int) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
""" | ||
Take in 2 integers. | ||
'a' is the integer to be arithmetically right shifted 'b' times. | ||
Return the shifted binary representation. | ||
|
||
>>> arithmetic_right_shift(0, 1) | ||
'0b00' | ||
>>> arithmetic_right_shift(1, 1) | ||
'0b00' | ||
>>> arithmetic_right_shift(-1, 1) | ||
'0b11' | ||
>>> arithmetic_right_shift(17, 2) | ||
'0b000100' | ||
>>> arithmetic_right_shift(-17, 2) | ||
'0b111011' | ||
>>> arithmetic_right_shift(-1983, 4) | ||
'0b111110000100' | ||
""" | ||
if a >= 0: | ||
binary_a = '0' + str(bin(a)).strip('-')[2:] | ||
else: | ||
binary_a = twos_complement(a)[2:] | ||
|
||
if b >= len(binary_a): | ||
return '0b' + binary_a[0] * len(binary_a) | ||
return '0b' + binary_a[0] * b + binary_a[:len(binary_a) - b] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def twos_complement(a: int) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
Take in a negative integer 'a'. | ||
Return the two's complement representation of 'a'. | ||
|
||
>>> twos_complement(0) | ||
'0b0' | ||
>>> twos_complement(-1) | ||
'0b11' | ||
>>> twos_complement(-5) | ||
'0b1011' | ||
>>> twos_complement(-17) | ||
'0b101111' | ||
>>> twos_complement(-207) | ||
'0b100110001' | ||
>>> twos_complement(1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: input must be a negative integer | ||
""" | ||
if a > 0: | ||
raise ValueError("input must be a negative integer") | ||
binary_a_length = len(bin(a)[3:]) | ||
twos_complement_a = bin(abs(a) - (1 << binary_a_length))[3:] | ||
twos_complement_a = ('1' + '0' * (binary_a_length - len(twos_complement_a)) + | ||
twos_complement_a) if a < 0 else '0' | ||
return '0b' + twos_complement_a | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
a
Please provide descriptive name for the parameter:
b