-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added binary_xor_operator.py and binary_and_operator.py #2433
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm | ||
|
||
def binary_and(a: int, b: int): | ||
""" | ||
Take in 2 integers, convert them to binary, | ||
return a binary number that is the | ||
result of a binary and operation on the integers provided. | ||
|
||
>>> binary_and(25, 32) | ||
'0b000000' | ||
>>> binary_and(37, 50) | ||
'0b100000' | ||
>>> binary_and(21, 30) | ||
'0b10100' | ||
>>> binary_and(58, 73) | ||
'0b0001000' | ||
>>> binary_and(0, 255) | ||
'0b00000000' | ||
>>> binary_and(256, 256) | ||
'0b100000000' | ||
>>> binary_and(0, -1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: the value of both input must be positive | ||
>>> binary_and(0, 1.1) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: 'float' object cannot be interpreted as an integer | ||
>>> binary_and("0", "1") | ||
Traceback (most recent call last): | ||
... | ||
TypeError: '<' not supported between instances of 'str' and 'int' | ||
""" | ||
if a < 0 or b < 0: | ||
raise ValueError("the value of both input must be positive") | ||
|
||
a_binary = str(bin(a))[2:] # remove the leading "0b" | ||
b_binary = str(bin(b))[2:] # remove the leading "0b" | ||
|
||
max_len = max(len(a_binary), len(b_binary)) | ||
|
||
return "0b" + "".join( | ||
str(int(char_a == "1" and char_b == "1")) | ||
for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len)) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm | ||
|
||
def binary_xor(a: int, b: int): | ||
""" | ||
Take in 2 integers, convert them to binary, | ||
return a binary number that is the | ||
result of a binary xor operation on the integers provided. | ||
|
||
>>> binary_xor(25, 32) | ||
'0b111001' | ||
>>> binary_xor(37, 50) | ||
'0b010111' | ||
>>> binary_xor(21, 30) | ||
'0b01011' | ||
>>> binary_xor(58, 73) | ||
'0b1110011' | ||
>>> binary_xor(0, 255) | ||
'0b11111111' | ||
>>> binary_xor(256, 256) | ||
'0b000000000' | ||
>>> binary_xor(0, -1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: the value of both input must be positive | ||
>>> binary_xor(0, 1.1) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: 'float' object cannot be interpreted as an integer | ||
>>> binary_xor("0", "1") | ||
Traceback (most recent call last): | ||
... | ||
TypeError: '<' not supported between instances of 'str' and 'int' | ||
""" | ||
if a < 0 or b < 0: | ||
raise ValueError("the value of both input must be positive") | ||
|
||
a_binary = str(bin(a))[2:] # remove the leading "0b" | ||
b_binary = str(bin(b))[2:] # remove the leading "0b" | ||
|
||
max_len = max(len(a_binary), len(b_binary)) | ||
|
||
return "0b" + "".join( | ||
str(int(char_a != char_b)) | ||
for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len)) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
why not just use
def binary_xor(a: int, b: int):
xor = a ^ b
return bin(xor)
would not it be faster or not?
and I don't understand why xor operators binary representation in your result differ than core xor operator:
for inputs:
37 ^ 50 = 0b10111
but in your version:
37 ^ 50 = 0b010111
yes of course you would be right if you say that leading zeros would not change the value but I think it would be better if we remove leading zeros after 0b ?
it's just my opinion, maybe @cclauss and others are agree with your version.
Uh oh!
There was an error while loading. Please reload this page.
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.
Often in this repo we are trying to demonstrate how an algorithm works. Our primary goal is education not always optimized performance. If we just use Python's builtin capabilities and its standard library we can often write code in a line or two but we lose a lot of educational value. Often the one or two-liner is great to use in our doctests to prove that the educational long-form gives the same results as the builtin short-form over a range of test values.
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.
The first thing using a^b gives us decimal representation on xor value
this function gives us a binary representation of xor function.
Totally agree with @cclauss ,this is just for learning and practicing things!
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.
But one more thing @cclauss does my PR is having any problems, because it is one week and it is not reviewed yet!?
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.
@cclauss ok, understand, thank you for your clarification