Skip to content

Commit 67991ed

Browse files
committed
Adding a Bit Manipulation Algorithm: Power of two numbers
1 parent 2e7089d commit 67991ed

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

BIT/Power of Two/CheckPowerOfTwo.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include<stdio.h>
2+
3+
int main()
4+
{
5+
int number,check;
6+
scanf("%d",&number);
7+
check= number & (number-1);
8+
if(check==0)
9+
printf("YES");
10+
else
11+
printf("NO");
12+
}

BIT/Power of Two/CheckPowerOfTwo.kt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fun main(args: Array<String>){
2+
var number = readLine()!!.toInt()
3+
var check = number and (number - 1)
4+
if(check == 0){
5+
print("Yes")
6+
}
7+
else{
8+
print("No")
9+
}
10+
}

BIT/Power of Two/IsPowerOfTwo.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
number = int(input())
2+
3+
isPowerOf2 = number & (number - 1)
4+
5+
if isPowerOf2 == 0:
6+
print('Yes')
7+
else:
8+
print('No')

BIT/Power of Two/NumberSwapper.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Demonstrates swapping of two number using bit manipulation.
3+
*/
4+
public class NumberSwapper {
5+
6+
private Integer x;
7+
private Integer y;
8+
9+
public NumberSwapper(Integer x, Integer y) {
10+
this.x = x;
11+
this.y = y;
12+
}
13+
14+
public Integer getX() {
15+
return x;
16+
}
17+
18+
public Integer getY() {
19+
return y;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "NumberSwapper{" +
25+
"x=" + x +
26+
", y=" + y +
27+
'}';
28+
}
29+
30+
/**
31+
* Swaps the values of fields x and y in class.
32+
*/
33+
public void swap() {
34+
x = x ^ y;
35+
y = x ^ y;
36+
x = x ^ y;
37+
}
38+
39+
/**
40+
* Swaps integers in array. Expects array to be not null and elements in array must be 2.
41+
* @param arr
42+
*/
43+
public static void swapArray(int[] arr) {
44+
checkNotNull(arr);
45+
checkState(arr);
46+
arr[0] = arr[0] ^ arr[1];
47+
arr[1] = arr[0] ^ arr[1];
48+
arr[0] = arr[0] ^ arr[1];
49+
}
50+
51+
private static void checkNotNull(int[] arr) {
52+
if(arr == null) {
53+
throw new NullPointerException("Array can not be null.");
54+
}
55+
}
56+
57+
private static void checkState(int[] arr) {
58+
if(arr.length != 2) {
59+
throw new IllegalStateException("Array size must be 2.");
60+
}
61+
}
62+
}

BIT/Power of Two/powerOfTwo.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
int number;
3+
int isPowerOfTwo(int number) {
4+
if ((number - 1) & number) {
5+
return 0;
6+
} else {
7+
return 1;
8+
}
9+
}
10+
int main() {
11+
scanf("%d", &number);
12+
printf("%d", isPowerOfTwo(number));
13+
}

BIT/Power of Two/powerOfTwo.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const isPowerOfTwo = number => {
2+
if (number === 0) {
3+
return 1;
4+
}
5+
number & (number - 1) === 0;
6+
}
7+
8+
const readline = require('readline');
9+
10+
const rl = readline.createInterface({
11+
input: process.stdin,
12+
output: process.stdout
13+
});
14+
15+
rl.question('Please input a number: ', (answer) => {
16+
console.log(isPowerOfTwo(answer) === 0);
17+
rl.close();
18+
});

0 commit comments

Comments
 (0)