Mega Code Archive

 
Categories / Java / Language Basics
 

Sets a specific bit of an int

/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ //package org.ancora.SharedLibrary; /**  * Methods for bit manipulation.  *  * @author Joao Bispo  */ public class Util{   private static final long MASK_16_BITS = 0xFFFFL;      private static final int MASK_BIT_1 = 0x1;      /**       * Sets a specific bit of an int.       *       * @param bit the bit to set. The least significant bit is bit 0       * @param target the integer where the bit will be set       * @return the updated value of the target       */      public static int setBit(int bit, int target) {         // Create mask         int mask = 1 << bit;         // Set bit         return target | mask;      } }