Skip to content

Commit be3d507

Browse files
committed
add bitand, bitor, bitxor templates
1 parent bd49fba commit be3d507

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/bitarray.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ proc `&`*(a, b: BitsArray): BitsArray =
111111
for i in 0 || (a.blocks-1):
112112
result.bits[i] = bitand(a.bits[i], b.bits[i])
113113

114+
template bitand*(a, b: BitsArray): BitsArray =
115+
a & b
116+
114117
proc `|`*(a, b: BitsArray): BitsArray =
115118
## Computes the bitwise or of a and b.
116119
##
@@ -120,20 +123,29 @@ proc `|`*(a, b: BitsArray): BitsArray =
120123
for i in 0 || (a.blocks-1):
121124
result.bits[i] = bitor(a.bits[i], b.bits[i])
122125

126+
template bitor*(a, b: BitsArray): BitsArray =
127+
a | b
128+
123129
proc `~`*(a: BitsArray): BitsArray =
124130
## Computes the bitwise not of a.
125131
##
126132
result = newBitsArray(a.len)
127133
for i in 0 || (a.blocks-1):
128134
result.bits[i] = bitnot(a.bits[i])
129135

136+
template bitnot*(a: BitsArray): BitsArray =
137+
~a
138+
130139
proc `^`*(a,b : BitsArray): BitsArray =
131140
## Computes the bitwise xor of a and b.
132141
##
133142
result = newBitsArray(a.len)
134143
for i in 0 || (a.blocks-1):
135144
result.bits[i] = bitxor(a.bits[i], b.bits[i])
136145

146+
template bitxor*(a,b : BitsArray): BitsArray =
147+
a ^ b
148+
137149
proc `[]`*(a: BitsArray, loc: int): bool=
138150
## Test bit value at loc.
139151
##
@@ -468,6 +480,7 @@ when isMainModule:
468480
echo "a & b = ", a & b
469481
echo "a | b = ", a | b
470482
echo "a ^ b = ", a ^ b
483+
echo "a bitxor b = ", a.bitxor(b)
471484
echo " ~a = ", ~a
472485
echo "a.shl(1) =",a.shl(1)
473486
echo "a.shl(2) =",a.shl(2)

0 commit comments

Comments
 (0)