Advertisement
eeperry

Java: Static method to read n bit from int

Mar 22nd, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.47 KB | None | 0 0
  1. /**
  2.      * readBit
  3.      * Determine if n bit is set.
  4.      * bit is 0 based
  5.      * @param value: the integer to read the bit from
  6.      * @param bit: the bit to read, 0 based
  7.      * @return true if bit is set; false if not set
  8.      */
  9.     public static boolean readBit(int value, int bit) {
  10.         int posVal = (int)Math.pow(2, (double)bit);
  11.         if ((value & posVal) == posVal) {
  12.             return true;
  13.         } else {
  14.             return false;
  15.         }
  16.     } // end readBit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement