Last active
May 31, 2018 17:39
-
-
Save nieldeokar/def39f6cf7b8d2be0f67011f9a40434a to your computer and use it in GitHub Desktop.
Demonstration of how to use 1 Integer object to store 32 boolean values
This file contains 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
public class AppUpdateCheckerUsingBitwiseShiftOperation { | |
private int mUpdateValue = 0; | |
private static final int UPDATE_AVAILABLE = 1; | |
private static final int UPDATE_COMPULSORY = 2; | |
public void setValue(int position){ | |
mUpdateValue = mUpdateValue | (1 << position); | |
// When position == UPDATE_AVAILABLE | |
// mUpdateValue = 0b0000 | (0b0001 << 1); // Do the Bitwise left shift by 1 | |
// mUpdateValue = 0b0000 | (0b0010); // Do the Bitwise OR | |
// mUpdateValue = 0b0010; | |
// When position == UPDATE_COMPULSORY | |
// mUpdateValue = 0b0010 | (0b0001 << 2); // Do the Bitwise left shift by 2 | |
// mUpdateValue = 0b0010 | (0b0100); // Do the Bitwise OR | |
// mUpdateValue = 0b0110; | |
} | |
public void getUpdate(int position){ | |
boolean result = false; | |
mUpdateValue = (mUpdateValue & (1 << position)) != 0; | |
// When position == UPDATE_AVAILABLE | |
// mUpdateValue = (0b0110 & (0b0001 << 1)) != 0; // Do the Bitwise left shift by 1 | |
// mUpdateValue = (0b0110 & (0b0010)) != 0; // Do the Bitwise AND | |
// mUpdateValue = (0b0010 != 0); // Compare output with 0 | |
// mUpdateValue = true; | |
// When position == UPDATE_COMPULSORY | |
// result = (0b0110 & (0b0001 << 2)) != 0; // Do the Bitwise left shift by 2 | |
// result = (0b0110 & (0b0100)) != 0; // Do the Bitwise AND | |
// result = (0b0100 != 0); // Compare output with 0 | |
// result = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment