Skip to content

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

Merged
merged 9 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions bit_manipulation/binary_shifts.py
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:
Copy link

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

"""
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:
Copy link

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

"""
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:
Copy link

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

"""
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()
33 changes: 33 additions & 0 deletions bit_manipulation/binary_twos_complement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def twos_complement(a: int) -> str:
Copy link

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

"""
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()