Java ByteBuffer Get getBit(ByteBuffer in, int pos)

Here you can find the source of getBit(ByteBuffer in, int pos)

Description

Get a bit from the input ByteBuffer Returns the bit at bit position pos in ByteBuffer in.

License

Open Source License

Parameter

Parameter Description
in The input ByteBuffer
pos The bit position in the given ByteBuffer

Return

Returns non-zero if the bit is set otherwise return zero.

Declaration

public static final int getBit(ByteBuffer in, int pos) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://w w w .j  ava 2s  .co  m
 *     IBM Corporation - Initial API and implementation
 *******************************************************************************/

import java.nio.ByteBuffer;

public class Main {
    /** A constant equal to the number of bits per byte */
    public static final int NBITS_PER_BYTE = 8;

    /**
     * Get a bit from the input ByteBuffer
     * 
     * Returns the bit at bit position pos in ByteBuffer in.
     * 
     * @param in
     *            The input ByteBuffer
     * @param pos
     *            The bit position in the given ByteBuffer
     * @return Returns non-zero if the bit is set otherwise return zero.
     */
    public static final int getBit(ByteBuffer in, int pos) {
        return in.get(pos >> 3) & (1 << (pos % NBITS_PER_BYTE));
    }
}

Related

  1. getAsciiString(ByteBuffer buffer)
  2. getAsIntArray(ByteBuffer yuv, int size)
  3. getAttrs(Map fields)
  4. getBatch(ByteBuffer bb)
  5. getBigDecimalFromByteBuffer( ByteBuffer bytebuf, int start, int length, int scale)
  6. getBitString(java.nio.ByteBuffer buffer, int lenBits)
  7. getBoolean(ByteBuffer b)
  8. getBoolean(ByteBuffer bb)
  9. getBoolean(ByteBuffer bytes)