Java Code Examples for com.helger.commons.CGlobal#BIT_SET

The following examples show how to use com.helger.commons.CGlobal#BIT_SET . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BitSetHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Extract the int representation of the passed bit set. To avoid loss of
 * data, the bit set may not have more than 32 bits.
 *
 * @param aBS
 *        The bit set to extract the value from. May not be <code>null</code>.
 * @return The extracted value. May be negative if the bit set has 32
 *         elements, the highest order bit is set.
 */
public static int getExtractedIntValue (@Nonnull final BitSet aBS)
{
  ValueEnforcer.notNull (aBS, "BitSet");

  final int nMax = aBS.length ();
  ValueEnforcer.isTrue (nMax <= CGlobal.BITS_PER_INT,
                        () -> "Can extract only up to " + CGlobal.BITS_PER_INT + " bits");

  int ret = 0;
  for (int i = nMax - 1; i >= 0; --i)
  {
    ret <<= 1;
    if (aBS.get (i))
      ret += CGlobal.BIT_SET;
  }
  return ret;
}
 
Example 2
Source File: BitSetHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Extract the long representation of the passed bit set. To avoid loss of
 * data, the bit set may not have more than 64 bits.
 *
 * @param aBS
 *        The bit set to extract the value from. May not be <code>null</code>.
 * @return The extracted value. May be negative if the bit set has 64
 *         elements, the highest order bit is set.
 */
public static long getExtractedLongValue (@Nonnull final BitSet aBS)
{
  ValueEnforcer.notNull (aBS, "BitSet");

  final int nMax = aBS.length ();
  ValueEnforcer.isTrue (nMax <= CGlobal.BITS_PER_LONG,
                        () -> "Can extract only up to " + CGlobal.BITS_PER_LONG + " bits");

  long ret = 0;
  for (int i = nMax - 1; i >= 0; --i)
  {
    ret <<= 1;
    if (aBS.get (i))
      ret += CGlobal.BIT_SET;
  }
  return ret;
}
 
Example 3
Source File: NonBlockingBitInputStream.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Read the next bit from the stream.
 *
 * @return 0 if the bit is 0, 1 if the bit is 1.
 * @throws IOException
 *         In case EOF is reached
 */
public int readBit () throws IOException
{
  if (m_aIS == null)
    throw new IOException ("BitInputStream is already closed");

  if (m_nNextBitIndex == CGlobal.BITS_PER_BYTE)
  {
    m_nBuffer = m_aIS.read ();
    if (m_nBuffer == -1)
      throw new EOFException ();

    m_nNextBitIndex = 0;
  }

  final int nSelectorBit = m_bHighOrderBitFirst ? (1 << (CGlobal.BITS_PER_BYTE - 1 - m_nNextBitIndex))
                                                : (1 << m_nNextBitIndex);
  final int nBitValue = m_nBuffer & nSelectorBit;
  m_nNextBitIndex++;

  return nBitValue == 0 ? CGlobal.BIT_NOT_SET : CGlobal.BIT_SET;
}
 
Example 4
Source File: NonBlockingBitOutputStream.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Write a single bit to the stream. It will only be flushed to the underlying
 * OutputStream when a byte has been completed or when flush() manually.
 *
 * @param aBit
 *        1 if the bit should be set, 0 if not
 * @throws IOException
 *         In case writing to the output stream failed
 */
public void writeBit (final int aBit) throws IOException
{
  if (m_aOS == null)
    throw new IllegalStateException ("BitOutputStream is already closed");
  if (aBit != CGlobal.BIT_NOT_SET && aBit != CGlobal.BIT_SET)
    throw new IllegalArgumentException (aBit + " is not a bit");

  if (aBit == CGlobal.BIT_SET)
    if (m_bHighOrderBitFirst)
      m_nBuffer |= (aBit << (7 - m_nBufferedBitCount));
    else
      m_nBuffer |= (aBit << m_nBufferedBitCount);

  ++m_nBufferedBitCount;
  if (m_nBufferedBitCount == CGlobal.BITS_PER_BYTE)
    flush ();
}