We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
nthBit
1 parent 7b7f4f4 commit 2b3bd16Copy full SHA for 2b3bd16
LowLevel/BitManip.scope
@@ -1,20 +1,15 @@
1
func str toBin(int num) {
2
- int i = 0;
3
str out = "";
4
- while (i < 64) {
5
- // Get only very right digit
6
- int v = and(shr(num, i), 1);
+ for (int i : 0..65) {
+ int v = nthBit(i, num);
7
8
// Insert 0 or 1
9
if (v == 0) {
10
out = "0" + out;
11
} else {
12
out = "1" + out;
13
}
14
-
15
- i += 1;
16
17
18
ret out;
19
20
@@ -38,6 +33,10 @@ func int rawCastDecToInt(dec a) {
38
33
ret o;
39
34
40
35
36
+func int nthBit(int n, int a) {
37
+ ret and(shr(a, n), 1);
+}
+
41
func int and(int a, int b) {
42
int o = 0;
43
assembly {
0 commit comments