Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2260a57

Browse filesBrowse files
committed
Implement bitwise and operator for type parameters.
1 parent 14241fd commit 2260a57
Copy full SHA for 2260a57

File tree

3 files changed

+41
-0
lines changed
Filter options

3 files changed

+41
-0
lines changed

‎compiler/expressions.go

Copy file name to clipboardExpand all lines: compiler/expressions.go
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ func (fc *funcContext) translateBinaryExpr(e *ast.BinaryExpr) *expression {
696696
return fc.formatExpr("%s.div(%e, %e)", fc.typeName(t), e.X, e.Y)
697697
case token.REM:
698698
return fc.formatExpr("%s.rem(%e, %e)", fc.typeName(t), e.X, e.Y)
699+
case token.AND:
700+
return fc.formatExpr("%s.and(%e, %e)", fc.typeName(t), e.X, e.Y)
699701
}
700702
}
701703

‎compiler/prelude/types.js

Copy file name to clipboardExpand all lines: compiler/prelude/types.js
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
425425
typ.mul = (x, y) => $truncateNumber(x * y, typ);
426426
typ.div = (x, y) => $idiv(x, y) >> 0;
427427
typ.rem = $irem;
428+
typ.and = (x, y) => (x & y);
428429
break;
429430
case $kindUint8:
430431
case $kindUint16:
@@ -433,6 +434,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
433434
typ.mul = (x, y) => $truncateNumber(x * y, typ);
434435
typ.div = (x, y) => $idiv(x, y) >>> 0;
435436
typ.rem = $irem;
437+
typ.and = (x, y) => (x & y) >>> 0;
436438
break;
437439
case $kindUint:
438440
case $kindUint32:
@@ -442,6 +444,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
442444
typ.mul = (x, y) => $imul(x, y) >>> 0;
443445
typ.div = (x, y) => $idiv(x, y) >>> 0;
444446
typ.rem = $irem;
447+
typ.and = (x, y) => (x & y) >>> 0;
445448
break;
446449
case $kindInt:
447450
case $kindInt32:
@@ -450,6 +453,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
450453
typ.mul = (x, y) => $imul(x, y);
451454
typ.div = (x, y) => $idiv(x, y) >> 0;
452455
typ.rem = $irem;
456+
typ.and = (x, y) => (x & y);
453457
break;
454458
case $kindInt64:
455459
case $kindUint64:
@@ -458,6 +462,7 @@ var $newType = (size, kind, string, named, pkg, exported, constructor) => {
458462
typ.mul = (x, y) => $mul64(x, y);
459463
typ.div = (x, y) => $div64(x, y, false);
460464
typ.rem = (x, y) => $div64(x, y, true);
465+
typ.and = (x, y) => new typ(x.$high & y.$high, (x.$low & y.$low) >>> 0);
461466
break;
462467
case $kindFloat32:
463468
case $kindFloat64:

‎tests/typeparams/arithmetics_test.go

Copy file name to clipboardExpand all lines: tests/typeparams/arithmetics_test.go
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,37 @@ func TestRemainder(t *testing.T) {
232232
t.Run(test.String(), test.Run)
233233
}
234234
}
235+
236+
func and[T constraints.Integer](x, y T) T {
237+
return x & y
238+
}
239+
240+
func andTC[T constraints.Integer](x, y, want T) *testCase[T] {
241+
return &testCase[T]{
242+
op: and[T],
243+
opName: token.AND,
244+
x: x,
245+
y: y,
246+
want: want,
247+
}
248+
}
249+
250+
func TestBitwiseAnd(t *testing.T) {
251+
tests := []testCaseI{
252+
andTC[int](0x0011, 0x0101, 0x0001),
253+
andTC[uint](0x0011, 0x0101, 0x0001),
254+
andTC[uintptr](0x0011, 0x0101, 0x0001),
255+
andTC[int8](0x11, 0x01, 0x01),
256+
andTC[int16](0x0011, 0x0101, 0x0001),
257+
andTC[int32](0x0011, 0x0101, 0x0001),
258+
andTC[uint8](0x11, 0x01, 0x01),
259+
andTC[uint16](0x0011, 0x0101, 0x0001),
260+
andTC[uint32](0x0011, 0x0101, 0x0001),
261+
andTC[int64](0x0000001100000011, 0x0000010100000101, 0x0000000100000001),
262+
andTC[uint64](0x0000001100000011, 0x0000010100000101, 0x0000000100000001),
263+
}
264+
265+
for _, test := range tests {
266+
t.Run(test.String(), test.Run)
267+
}
268+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.