Skip to content

Instantly share code, notes, and snippets.

@Komet-Kazi
Last active April 10, 2021 21:10
Show Gist options
  • Select an option

  • Save Komet-Kazi/e03cc6f76886088a3647574af81855c6 to your computer and use it in GitHub Desktop.

Select an option

Save Komet-Kazi/e03cc6f76886088a3647574af81855c6 to your computer and use it in GitHub Desktop.
Binary Operations
# The bit position in a binary integer giving the units value, that is, determining whether the number is even or odd.
# The LSB is sometimes referred to as the low-order bit or right-most bit
# the most significant bit (MSB, also called the high-order bit) is the bit position in a binary number having the greatest value.
# The MSB is sometimes referred to as the high-order bit or left-most bit.
# "1" signifies a negative number and "0" signifies a positive number
# Example:
# if bite is currently b'0 0 0 0 0 0 0 1,
# bite << 1 would produce: b'0 0 0 0 0 0 1 0
# From: b'0 0 0 0 0 0 1 0
# To: b'0 0 0 0 0 1 0 0
# Bitwise AND
# The bitwise AND operator (&) performs logical conjunction on the corresponding bits of its operands. For each pair of bits occupying the same position in the two numbers, it returns a one only when both bits are switched on:
# The resulting bit pattern is an intersection of the operator’s arguments. It has two bits turned on in the positions where both operands are ones. In all other places, at least one of the inputs has a zero bit.
bin(156) & bin(52)
# Bitwise and
# Bits turned on in the positions where both operands are ones. In all other places, at least one of the inputs has a zero bit.
class BitWise:
def __init__(self):
# Label my LEDS via
self.LED0 = 0b00000001
self.LED1 = 0b00000010
self.LED2 = 0b00000100
self.LED3 = 0b00001000
self.LED4 = 0b00010000
self.LED5 = 0b00100000
self.LED6 = 0b01000000
self.LED7 = 0b10000000
self.LEDS = [self.LED0, self.LED1, self.LED2, self.LED3, self.LED4, self.LED5, self.LED6, self.LED7]
def bitAND(self):
"""Bits turned on in the positions where both operands are ones. In all other places, at least one of the inputs has a zero bit."""
results = []
for j in range(8):
for i in range(8):
bit_and = self.LEDS[j] & self.LEDS[i]
results.append(f'LEDs:\t{j}, {i}\n\t{hex(self.LEDS[j])} & {hex(self.LEDS[i])} = {hex(bit_and), bin(bit_and)}')
return '\n'.join(results)
def bitOR(self):
"""Bits turned on where either of the operands has a one. Only a combination of two zeros gives a zero in the final output."""
results = []
for j in range(8):
for i in range(8):
bit_or = self.LEDS[j] | self.LEDS[i]
results.append(f'LEDs:\t{j}, {i}\n\t{hex(self.LEDS[j])} | {hex(self.LEDS[i])} = {hex(bit_or), bin(bit_or)}')
return '\n'.join(results)
def bitXOR(self):
"""Every bit pair must contain opposing bit values to produce a one"""
results = []
for j in range(8):
for i in range(8):
bit_xor = xor(self.LEDS[j], self.LEDS[i])
results.append(f'LEDs:\t{j}, {i}\n\t{hex(self.LEDS[j])} ^ {hex(self.LEDS[i])} = {hex(bit_xor), bin(bit_xor)}')
return '\n'.join(results)
def xor(a,b):
"""every bit pair must contain opposing bit values to produce a one"""
return (a and not b) or (not a and b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment