mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Add Spanish link for the Linked List README.
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
# Is a power of two
|
||||
|
||||
Given a positive integer, write a function to find if it is
|
||||
Given a positive integer, write a function to find if it is
|
||||
a power of two or not.
|
||||
|
||||
**Naive solution**
|
||||
|
||||
In naive solution we just keep dividing the number by two
|
||||
unless the number becomes `1` and every time we do so we
|
||||
check that remainder after division is always `0`. Otherwise
|
||||
the number can't be a power of two.
|
||||
unless the number becomes `1` and every time we do so, we
|
||||
check that remainder after division is always `0`. Otherwise, the number can't be a power of two.
|
||||
|
||||
**Bitwise solution**
|
||||
|
||||
@@ -23,8 +22,8 @@ signed integer with a value of -128 looks like: `10000000`)
|
||||
8: 1000
|
||||
```
|
||||
|
||||
So after checking that the number is greater than zero,
|
||||
we can use a bitwise hack to test that one and only one
|
||||
So after checking that the number is greater than zero,
|
||||
we can use a bitwise hack to test that one and only one
|
||||
bit is set.
|
||||
|
||||
```
|
||||
@@ -38,11 +37,11 @@ For example for number `8` that operations will look like:
|
||||
- 0001
|
||||
----
|
||||
0111
|
||||
|
||||
|
||||
1000
|
||||
& 0111
|
||||
----
|
||||
0000
|
||||
0000
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
Reference in New Issue
Block a user