Last active
May 1, 2022 06:17
-
-
Save shah-smit/8b81ac585544e3e5c276bb91d20a302e to your computer and use it in GitHub Desktop.
Bit Wise Java Beginners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.quiz.box.quizservice; | |
/** | |
* Notes: ~0 is equals to -1 | |
*/ | |
public class HelloBitWise { | |
public static void main(String[] args) { | |
System.out.println("Print Whether a number is Odd or Even:"); | |
printOddOrEven(5); | |
printOddOrEven(10); | |
System.out.println(); | |
System.out.println("Clearing the bits:"); | |
System.out.println(clearBit(5, 0)); //Outputs 4 | |
System.out.println(clearBit(5, 1)); //Outputs 5 | |
System.out.println(clearBit(5, 2)); //Outputs 1 | |
System.out.println("Setting the bits:"); | |
setBit(5, 0); //Outputs 5 | |
setBit(5, 1); //Outputs 7 | |
setBit(5, 2); //Outputs 5 | |
System.out.println("Shifting left the bits:"); | |
/** | |
* 5: 0 1 0 1 | |
* Shift 0 hence no change, hence output is 5 | |
* Shift 1 hence moving all to one left, 1 0 1 0, hence output is 10 | |
* Shift 2 hence moving all to two left, 1 0 1 0 0, hence output is 20. | |
* Note: In Java, 1 decimal is 8 bits as such the above Shift 2, | |
* does not drop the bit instead it kept moving left. | |
*/ | |
shiftLeft(5, 0); //Outputs 5 | |
shiftLeft(5, 1); //Outputs 10 | |
shiftLeft(5, 2); //Outputs 20 | |
System.out.println("Shifting right the bits:"); | |
/** | |
* 5: 0 1 0 1 | |
* Shift 0 hence no change, hence output is 5 | |
* Shift 1 hence moving all to one position right, 0 0 1 0, hence output is 2 | |
* Shift 2 hence moving all to two position left, 0 0 0 1, hence output is 1. | |
*/ | |
shiftRight(5, 0); //Outputs 5 | |
shiftRight(5, 1); //Outputs 2 | |
shiftRight(5, 2); //Outputs 1 | |
System.out.println("Printing the ith bit:"); | |
printIthBit(5, 1); | |
System.out.println("Updating the ith bit:"); | |
/** | |
* 13: 1 1 0 1 | |
* Update the 1st position to 1, from current 0. Hence, 1 1 1 1 translates to 15 | |
* Update the 0th Position to 0, from current 1. Hence, 1 1 0 0 translates to 12 | |
* Update the 3rd Position to 0, from current 1. Hence, 0 1 0 1 translates to 5 | |
*/ | |
updateIthBit(13, 1, 1); //Prints 15 | |
updateIthBit(13, 0, 0); //Prints 12 | |
updateIthBit(13, 3, 0); //Prints 5 | |
System.out.println("Clearing all the bits from the ith bit:"); | |
/** | |
* Udemy: https://nlbsg.udemy.com/course/competitive-programming-algorithms-coding-minutes/learn/lecture/28250296#notes | |
* | |
*/ | |
clearLastIthBits(15, 3); | |
System.out.println("Clearing bits in range:"); | |
System.out.println(clearBitsFromRange(31, 1, 3)); | |
/** | |
* 1 1 1 1 <- Given | |
* 1 X X 1 <- Xs are the one that needs to be removed meaning if 0 keep 0, if 1 turn it 0 | |
* Now Aglo | |
* 1 0 0 0 <- variable a which will convert all from left to jth index to 1 | |
* 0 0 0 1 <- variable b where will convert all from right to ith index to 1 | |
* 1 0 0 1 <- this is a OR of the above, which will give us a MASK | |
* | |
* Now we will perform a AND with mask and given | |
* 1 1 1 1 | |
* & 1 0 0 1 | |
* ----------------------- | |
* 1 0 0 1 the result! | |
*/ | |
System.out.println(clearBitsFromRange(15, 1, 2)); | |
/** | |
* Problem Statement: | |
* You are given two 32-bit numbers, N and M, and two bit positions i and j. | |
* Write a method to set all bits between i and j in N equal to M | |
* M (becomes a substring of N located at and starting at j) | |
* | |
* Example: | |
* N = 10000000000 | |
* M = 10101 | |
* i = 2, j = 6 | |
* Output: 1001010100 | |
* | |
* Algo | |
* Clearing the range of bits from ith of jth for N variable | |
* 100XXXXXX00 | |
* Add Xeros bit at the end of the M variable such that we can perform a OR operatior | |
* 1010100 | |
* Now perform OR of the above | |
* 1 0 0 X X X X X 0 0 | |
* | 0 0 0 1 0 1 0 1 0 0 | |
* ------------------- | |
* 1 0 0 1 0 1 0 1 0 0 | |
*/ | |
replaceBits(15, 1, 3, 2); | |
System.out.println("Is a number a power of 2?"); | |
System.out.println("Is 16 a power of 2? " + isNumberAPowerOf2(16) ); | |
System.out.println("Is 5 a power of 2? " + isNumberAPowerOf2(5)); | |
} | |
private static void printOddOrEven(int num) { | |
if ((num & 1) == 1) { | |
System.out.println("Odd"); | |
} else { | |
System.out.println("Even"); | |
} | |
} | |
/** | |
* @param n is the actual number to manupulate | |
* @param i is the index at which will be FORCED set to 0, be it 1 or 0 in given number | |
*/ | |
private static int clearBit(int n, int i) { | |
int mask = ~(1 << i); | |
n = n & mask; | |
return n; | |
} | |
/** | |
* @param n is the actual number to manupulate | |
* @param i is the index at which will be FORCED set to 1, be it 1 or 0 in given number | |
*/ | |
private static void setBit(int n, int i) { | |
int mask = 1 << i; | |
System.out.println(n | mask); | |
} | |
private static void shiftLeft(int n, int i) { | |
System.out.println(n << i); | |
} | |
private static void shiftRight(int n, int i) { | |
System.out.println(n >> i); | |
} | |
private static void printIthBit(int n, int i) { | |
int mask = 1 << i; | |
System.out.println((n & mask) > 0 ? 1 : 0); | |
} | |
private static void updateIthBit(int n, int i, int newBit) { | |
n = clearBit(n, i); | |
int mask = newBit << i; | |
n = n | mask; | |
System.out.println(n); | |
} | |
/** | |
* @param n | |
* @param i from this index to the right, all will be cleared | |
*/ | |
private static void clearLastIthBits(int n, int i) { | |
int mask = (-1 << i); | |
n = n & mask; | |
System.out.println(n); | |
} | |
/** | |
* @param n | |
* @param i index on the right | |
* @param j index on the left | |
*/ | |
private static int clearBitsFromRange(int n, int i, int j) { | |
/** | |
* 1 1 1 0 1 0 1 0 1 1 1 0 <- Given | |
* 1 1 1 1 1 X X X X X 1 0 <- Xs are the one that needs to be removed meaning if 0 keep 0, if 1 turn it 0 | |
* Now Aglo | |
* 1 1 1 1 1 0 0 0 0 0 0 0 <- variable a which will convert all from left to jth index to 1 | |
* 0 0 0 0 0 0 0 0 0 0 1 1 <- variable b where will convert all from right to ith index to 1 | |
* 1 1 1 1 1 0 0 0 0 0 1 1 <- this is a OR of the above, which will give us a MASK | |
* | |
* Now we will perform a AND with mask and given | |
* 1 1 1 0 1 0 1 0 1 1 1 0 | |
* & 1 1 1 1 1 0 0 0 0 0 1 1 | |
* ----------------------- | |
* 1 1 1 0 1 0 0 0 0 0 1 0 the result! | |
*/ | |
int a = (~0) << (j + 1); | |
int b = (1 << i) - 1; | |
int mask = a | b; | |
n = n & mask; | |
return n; | |
} | |
public static void replaceBits(int n, int i, int j, int m) { | |
n = clearBitsFromRange(n, i, j); | |
m = m << i; | |
int res = n | m; | |
System.out.println(res); | |
} | |
public static boolean isNumberAPowerOf2(int n) { | |
/** | |
* https://leetcode.com/problems/power-of-two | |
* n = 16 | |
* 1 0 0 0 0 | |
* & 1 1 1 1 | |
* ---------- | |
* 0 0 0 0 0 | |
* If 0 it will be a power of 2! | |
* | |
* n = 5 | |
* 1 0 1 | |
* & 1 0 0 | |
* -------- | |
* 1 0 0 Hence it is not a power of 2! | |
*/ | |
if (n == 0) { | |
return false; | |
} | |
if (n == -2147483648) { | |
return false; | |
} | |
if ((n & n - 1) == 0) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment