Skip to content

Bit Manipulation #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Java/5_Bit Manipulation/BitOperations.class
Binary file not shown.
80 changes: 80 additions & 0 deletions Java/5_Bit Manipulation/BitOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
public class BitOperations {
//Get ith Bit
public static int getIthBit(int n, int i) {
int bitMask = 1<<i;
return (n & bitMask) == 0 ? 0 : 1;
}

//Set ith Bit
public static int setIthBit(int n, int i) {
int bitMask = 1<<i;
return n | bitMask;
}

//Clear ith Bit
public static int clearIthBit(int n, int i) {
int bitMask = ~(1<<i);
return n & bitMask;
}

//Update ith Bit
public static int updateIthBit(int n, int i, int val) {
//Method1
// if(val == 0) {
// return n & (~(1<<i));
// } else {
// return n | (1<<i);
// }

//Method2
n = clearIthBit(n, i);
int bitMask = val<<i;
return n | bitMask;
}

//Clear Last i Bits
public static int clearLastIbits(int n, int i) {
int bitMask = (~0)<<i; // or (-1)<<i
return n | bitMask;
}

//Clear Bits in Range (i,j)
public static int clearBitsinRange(int n, int i, int j) {
int a = ~0<<i;
int b = (1<<i)-1;
int bitMask = a | b;
return n & bitMask;
}

//Check if number is power of 2 or not
public static boolean isPowerof2(int n) {
return (n & (n-1)) == 0;
}

//Count number of set bits
public static int countSetBits(int n) {
int setBits = 0;
int bitMask = 1;
while(n != 0) {
if((n & bitMask) != 0) { //last bit is 1
setBits++;
}
n = n>>1;
}
return setBits;
}

//a faster method to count set bits
public static int countSetBits2(int n) {
int setBits = 0;
while(n>0) {
// removes the last set bit from curr number
n = n & (n-1);
setBits++;
}
return setBits;
}
public static void main(String args[]) {
System.out.println(countSetBits(9));
}
}
6 changes: 6 additions & 0 deletions Java/5_Bit Manipulation/BitwiseOperators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class BitwiseOperators {
public static void main(String args[]) {
int a = 0;
System.out.println(~a);
}
}
Binary file added Java/5_Bit Manipulation/FastExponentiation.class
Binary file not shown.
19 changes: 19 additions & 0 deletions Java/5_Bit Manipulation/FastExponentiation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class FastExponentiation {
//to calculate a^n
public static int pow(int a, int n) {
int ans = 1;

while(n > 0) {
if((n&1) != 0) { //checking bit is 0 or 1
ans = ans * a;
}
a = a*a;
n = n>>1;
}

return ans;
}
public static void main(String args[]) {
System.out.println(pow(5,3));
}
}
11 changes: 11 additions & 0 deletions Java/5_Bit Manipulation/OddEven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class OddEven {
public static void main(String args[]) {
int n = 10;
int bitMask = 1;
if((n & bitMask) == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
}
}