Skip to content

Commit 2b3bd16

Browse files
committed
Added nthBit
1 parent 7b7f4f4 commit 2b3bd16

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

LowLevel/BitManip.scope

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
func str toBin(int num) {
2-
int i = 0;
32
str out = "";
4-
while (i < 64) {
5-
// Get only very right digit
6-
int v = and(shr(num, i), 1);
3+
for (int i : 0..65) {
4+
int v = nthBit(i, num);
75

86
// Insert 0 or 1
97
if (v == 0) {
108
out = "0" + out;
119
} else {
1210
out = "1" + out;
1311
}
14-
15-
i += 1;
1612
}
17-
1813
ret out;
1914
}
2015

@@ -38,6 +33,10 @@ func int rawCastDecToInt(dec a) {
3833
ret o;
3934
}
4035

36+
func int nthBit(int n, int a) {
37+
ret and(shr(a, n), 1);
38+
}
39+
4140
func int and(int a, int b) {
4241
int o = 0;
4342
assembly {

0 commit comments

Comments
 (0)