/ console / types / boolean / src / bitwise.rs
bitwise.rs
  1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
  2  // This file is part of the alphavm library.
  3  
  4  // Licensed under the Apache License, Version 2.0 (the "License");
  5  // you may not use this file except in compliance with the License.
  6  // You may obtain a copy of the License at:
  7  
  8  // http://www.apache.org/licenses/LICENSE-2.0
  9  
 10  // Unless required by applicable law or agreed to in writing, software
 11  // distributed under the License is distributed on an "AS IS" BASIS,
 12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  // See the License for the specific language governing permissions and
 14  // limitations under the License.
 15  
 16  use super::*;
 17  
 18  impl<E: Environment> Equal for Boolean<E> {
 19      type Output = Boolean<E>;
 20  
 21      /// Returns `true` if `self` and `other` are equal.
 22      fn is_equal(&self, other: &Self) -> Self::Output {
 23          Boolean::new(self == other)
 24      }
 25  
 26      /// Returns `true` if `self` and `other` are *not* equal.
 27      fn is_not_equal(&self, other: &Self) -> Self::Output {
 28          Boolean::new(self != other)
 29      }
 30  }
 31  
 32  impl<E: Environment> Not for Boolean<E> {
 33      type Output = Boolean<E>;
 34  
 35      /// Returns the `negation` of `self`.
 36      #[inline]
 37      fn not(self) -> Self::Output {
 38          Boolean::new(!self.boolean)
 39      }
 40  }
 41  
 42  impl<E: Environment> BitAnd<Boolean<E>> for Boolean<E> {
 43      type Output = Boolean<E>;
 44  
 45      /// Returns the bitwise `AND`` of `self` and `other`.
 46      #[inline]
 47      fn bitand(self, other: Self) -> Self::Output {
 48          Boolean::new(self.boolean & other.boolean)
 49      }
 50  }
 51  
 52  impl<E: Environment> BitAnd<&Boolean<E>> for Boolean<E> {
 53      type Output = Boolean<E>;
 54  
 55      /// Returns the bitwise `AND` of `self` and `other`.
 56      #[inline]
 57      fn bitand(self, other: &Boolean<E>) -> Self::Output {
 58          Boolean::new(self.boolean & other.boolean)
 59      }
 60  }
 61  
 62  impl<E: Environment> BitAndAssign for Boolean<E> {
 63      /// Sets `self` as the bitwise `AND` of `self` and `other`.
 64      #[inline]
 65      fn bitand_assign(&mut self, other: Self) {
 66          *self = Boolean::new(self.boolean & other.boolean)
 67      }
 68  }
 69  
 70  impl<E: Environment> BitOr for Boolean<E> {
 71      type Output = Boolean<E>;
 72  
 73      /// Returns the bitwise `OR` of `self` and `other`.
 74      #[inline]
 75      fn bitor(self, other: Self) -> Self::Output {
 76          Boolean::new(self.boolean | other.boolean)
 77      }
 78  }
 79  
 80  impl<E: Environment> BitOr<&Boolean<E>> for Boolean<E> {
 81      type Output = Boolean<E>;
 82  
 83      /// Returns the bitwise `OR` of `self` and `other`.
 84      #[inline]
 85      fn bitor(self, other: &Boolean<E>) -> Self::Output {
 86          Boolean::new(self.boolean | other.boolean)
 87      }
 88  }
 89  
 90  impl<E: Environment> BitOrAssign for Boolean<E> {
 91      /// Sets `self` as the bitwise `OR` of `self` and `other`.
 92      #[inline]
 93      fn bitor_assign(&mut self, other: Self) {
 94          *self = Boolean::new(self.boolean | other.boolean)
 95      }
 96  }
 97  
 98  impl<E: Environment> BitXor for Boolean<E> {
 99      type Output = Boolean<E>;
100  
101      /// Returns the bitwise `XOR` of `self` and `other`.
102      #[inline]
103      fn bitxor(self, other: Self) -> Self::Output {
104          Boolean::new(self.boolean ^ other.boolean)
105      }
106  }
107  
108  impl<E: Environment> BitXor<&Boolean<E>> for Boolean<E> {
109      type Output = Boolean<E>;
110  
111      /// Returns the bitwise `XOR` of `self` and `other`.
112      #[inline]
113      fn bitxor(self, other: &Boolean<E>) -> Self::Output {
114          Boolean::new(self.boolean ^ other.boolean)
115      }
116  }
117  
118  impl<E: Environment> BitXorAssign for Boolean<E> {
119      /// Sets `self` as the bitwise `XOR` of `self` and `other`.
120      #[inline]
121      fn bitxor_assign(&mut self, other: Self) {
122          *self = Boolean::new(self.boolean ^ other.boolean)
123      }
124  }
125  
126  impl<E: Environment> Nand for Boolean<E> {
127      type Output = Boolean<E>;
128  
129      /// Returns the bitwise `NAND` of `self` and `other`.
130      #[inline]
131      fn nand(&self, other: &Self) -> Self::Output {
132          Boolean::new(!(self.boolean & other.boolean))
133      }
134  }
135  
136  impl<E: Environment> Nor for Boolean<E> {
137      type Output = Boolean<E>;
138  
139      /// Returns the bitwise `NOR` of `self` and `other`.
140      #[inline]
141      fn nor(&self, other: &Self) -> Self::Output {
142          Boolean::new(!(self.boolean | other.boolean))
143      }
144  }
145  
146  impl<E: Environment> Ternary for Boolean<E> {
147      type Boolean = Boolean<E>;
148      type Output = Self;
149  
150      /// Returns `first` if `condition` is `true`, otherwise returns `second`.
151      fn ternary(condition: &Self::Boolean, first: &Self, second: &Self) -> Self::Output {
152          match **condition {
153              true => *first,
154              false => *second,
155          }
156      }
157  }