Mega Code Archive

 
Categories / Java / Language Basics
 

Returns 16 bits from the long number

/*  * 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;      /**       * Returns 16 bits from the long number.       *        * @param data       * @param offset one of 0 to 3       * @return       */      public static int get16BitsAligned(long data, int offset) {         // Normalize offset         offset = offset%4;         //System.out.println("offset:"+offset);         // Align the mask         long mask = MASK_16_BITS << 16*offset;         //System.out.println("Mask:"+Long.toHexString(mask));         //System.out.println("Data:"+Long.toHexString(data));         // Get the bits         long result = data & mask;         // Put bits in position         return (int) (result >>> (16*offset));      } }