Advertisement
eeperry

Java: Static method to set n bit of int

Mar 22nd, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.28 KB | None | 0 0
  1. /**
  2.      * Set n bit in the integer value.
  3.      * bit is 0 based.
  4.      * @param value
  5.      * @param bit
  6.      * @return value with the n bit set
  7.      */
  8.     public static int setBit(int value, int bit) {
  9.         int posVal = (int)Math.pow(2, (double)bit);
  10.         return value | posVal;
  11.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement