diff --git a/Java/5_Bit Manipulation/BitOperations.class b/Java/5_Bit Manipulation/BitOperations.class new file mode 100644 index 00000000..c861dd27 Binary files /dev/null and b/Java/5_Bit Manipulation/BitOperations.class differ diff --git a/Java/5_Bit Manipulation/BitOperations.java b/Java/5_Bit Manipulation/BitOperations.java new file mode 100644 index 00000000..74a6bbf8 --- /dev/null +++ b/Java/5_Bit Manipulation/BitOperations.java @@ -0,0 +1,80 @@ +public class BitOperations { + //Get ith Bit + public static int getIthBit(int n, int i) { + int bitMask = 1<>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)); + } +} diff --git a/Java/5_Bit Manipulation/BitwiseOperators.java b/Java/5_Bit Manipulation/BitwiseOperators.java new file mode 100644 index 00000000..e7a09fe7 --- /dev/null +++ b/Java/5_Bit Manipulation/BitwiseOperators.java @@ -0,0 +1,6 @@ +public class BitwiseOperators { + public static void main(String args[]) { + int a = 0; + System.out.println(~a); + } +} \ No newline at end of file diff --git a/Java/5_Bit Manipulation/FastExponentiation.class b/Java/5_Bit Manipulation/FastExponentiation.class new file mode 100644 index 00000000..efae5c79 Binary files /dev/null and b/Java/5_Bit Manipulation/FastExponentiation.class differ diff --git a/Java/5_Bit Manipulation/FastExponentiation.java b/Java/5_Bit Manipulation/FastExponentiation.java new file mode 100644 index 00000000..c4b03583 --- /dev/null +++ b/Java/5_Bit Manipulation/FastExponentiation.java @@ -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)); + } +} diff --git a/Java/5_Bit Manipulation/OddEven.java b/Java/5_Bit Manipulation/OddEven.java new file mode 100644 index 00000000..3af2c577 --- /dev/null +++ b/Java/5_Bit Manipulation/OddEven.java @@ -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"); + } + } +}