From b2126652f66a9446fb7275fdc1e92d95e194baa3 Mon Sep 17 00:00:00 2001 From: Nishitbariya <85815172+Nishitbaria@users.noreply.github.com> Date: Sun, 2 Oct 2022 10:04:39 +0530 Subject: [PATCH] BitManipulation Problem Statement Solutions Of Bit Manipulation --- Java/5_Bit Manipulation/BitOperations.class | Bin 0 -> 1106 bytes Java/5_Bit Manipulation/BitOperations.java | 80 ++++++++++++++++++ Java/5_Bit Manipulation/BitwiseOperators.java | 6 ++ .../FastExponentiation.class | Bin 0 -> 555 bytes .../FastExponentiation.java | 19 +++++ Java/5_Bit Manipulation/OddEven.java | 11 +++ 6 files changed, 116 insertions(+) create mode 100644 Java/5_Bit Manipulation/BitOperations.class create mode 100644 Java/5_Bit Manipulation/BitOperations.java create mode 100644 Java/5_Bit Manipulation/BitwiseOperators.java create mode 100644 Java/5_Bit Manipulation/FastExponentiation.class create mode 100644 Java/5_Bit Manipulation/FastExponentiation.java create mode 100644 Java/5_Bit Manipulation/OddEven.java diff --git a/Java/5_Bit Manipulation/BitOperations.class b/Java/5_Bit Manipulation/BitOperations.class new file mode 100644 index 0000000000000000000000000000000000000000..c861dd27c7a3ca855b1d15e724cb14bf5f2cd5fc GIT binary patch literal 1106 zcmZuw-A)rh7(LT&yL4Gf0gJk&{3u`x2vHNFv@}E#lO`y{8WQzt+pf6S?l$dK(F;S2 zFM%(>jW>Fs#0ww5m+=MsJF~mAXiSs&rZaPXzB9Ametr20pok?65kyreI%0?mr1s6{ z=1RkK_Exs5`&P{ph!<_g_DTZLTz*%>01_%R9fQyXhHl&5cFSs;p6xmvfx%kCGTUYE zDc1#JxpFyQP9TY46)7Df82wA3^0MPuO%l6Zo=$K1+P1s0)3zP2;4zR(W(yJ_Xj4k~vs6CSJdT8a;-h zyysfPN6E(mDE!4R%^4+sASpH^&rFYJHsl!ycn&2#Pz~@J@qGv{8+!Q03CU&-`*K-*0J3WI69L|w> zo`|`gn#?9nwQ(X!bg9%=m26r$T#Xs&*crE@zNbZwOYH9|Juj1I9#=xw*XT+npEL}m zE)!OmaI~Lra4$FfbYqC<8!}ox{t*5!49bWWm389?V~pwu@wY-4%B#Zh@9&vD)g)93 z-6Y}`?X2;#3j7j_v{d3Zv*Al55d8^72t7`j9P*(|jv(n$|A?Vf;sj%#aK8U`f{ZrV N-yNP>;JFAE{{XIft5g60 literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..efae5c79a58dc382a1dfbbe9a932bc5d21249be5 GIT binary patch literal 555 zcmZva%}&BV5Xb*pN*78I6cwcSbt@j^0RS&X6B54?j2BM}Ho6Ec!NsqG@8QL(7cY8H zDT~4vQh%5yzs^6~TUGv(@q2 ziovSMK&mB%m{;COB7r1_?H~o`S9 z?~qvz_k4!TnhgAnVXx&6wneK;f6`4Q+K20+{~en>AxSpr?S9cl$0k%6>{oRmyBQ7# zZGTm2asDstqUOOo9m=3#0x=LcELx{1nkLK0#upyIZpmRN(yF($pVTuTt5bLNC6Jvq z>+Tf_u6c0@UMe2ci^YSgYo6DxKVC~Wlwd?9X28ieDYEF}aOhI9(8Ef>B^sA@6NtUR mU}>8gl_D11$cq>O=MFq!JtBRN+z*<53m~ji1U5syf!R-I-DhY3 literal 0 HcmV?d00001 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"); + } + } +}