nitlanguage
/
nit.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
(from parent 1:
87d22cf
)
Merge: Int::is_pow2
author
Jean Privat
<jean@pryen.org>
Sun, 29 May 2016 20:35:56 +0000
(16:35 -0400)
committer
Jean Privat
<jean@pryen.org>
Sun, 29 May 2016 20:35:56 +0000
(16:35 -0400)
Simple addition to `core`, the `is_pow2` method on Int tells whether an integer is a power of two.
Pull-Request: #2136
Reviewed-by: Jean Privat <jean@pryen.org>
lib/core/math.nit
patch
|
blob
|
history
diff --git
a/lib/core/math.nit
b/lib/core/math.nit
index
0f8325f
..
7b51e2a
100644
(file)
--- a/
lib/core/math.nit
+++ b/
lib/core/math.nit
@@
-169,6
+169,16
@@
redef class Int
end
return res
end
+
+ # Is `self` a power of two ?
+ #
+ # ~~~nit
+ # assert not 3.is_pow2
+ # assert 2.is_pow2
+ # assert 1.is_pow2
+ # assert not 0.is_pow2
+ # ~~~
+ fun is_pow2: Bool do return self != 0 and (self & self - 1) == 0
end
redef class Byte