Skip to content

Commit

Permalink
Merge pull request #14 from MichaelBell/fix-logic-operators
Browse files Browse the repository at this point in the history
Allow logic operators with int
  • Loading branch information
psychogenic authored Nov 24, 2024
2 parents 95145f3 + f8f8b87 commit 35d624e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ttboard/types/logic_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ def __index__(self) -> int:
return int(self)

def __and__(self, other: "LogicArray") -> "LogicArray":
if isinstance(other, int):
# Allow &= ~x to work
other &= (1 << len(self)) - 1
return LogicArray(value=(other & self.to_unsigned()), range=self._range)
if not isinstance(other, LogicArray):
return NotImplemented
if len(self) != len(other):
Expand All @@ -368,6 +372,8 @@ def __and__(self, other: "LogicArray") -> "LogicArray":
return LogicArray(a & b for a, b in zip(self, other))

def __or__(self, other: "LogicArray") -> "LogicArray":
if isinstance(other, int):
return LogicArray(value=(other | self.to_unsigned()), range=self._range)
if not isinstance(other, LogicArray):
return NotImplemented
if len(self) != len(other):
Expand All @@ -377,6 +383,8 @@ def __or__(self, other: "LogicArray") -> "LogicArray":
return LogicArray(a | b for a, b in zip(self, other))

def __xor__(self, other: "LogicArray") -> "LogicArray":
if isinstance(other, int):
return LogicArray(value=(other ^ self.to_unsigned()), range=self._range)
if not isinstance(other, LogicArray):
return NotImplemented
if len(self) != len(other):
Expand Down

0 comments on commit 35d624e

Please sign in to comment.