Coverage Report - org.jscsi.target.util.BitManip
 
Classes in this File Line Coverage Branch Coverage Complexity
BitManip
0%
0/17
0%
0/4
2.5
 
 1  
 package org.jscsi.target.util;
 2  
 
 3  
 /**
 4  
  * A utility class with static methods for accessing individual bits of bytes.
 5  
  * <p>
 6  
  * The value of a single bit can be easily retrieved with the {@link #getBit(byte, int)} method.
 7  
  * <p>
 8  
  * Setting a bit is a little bit more complicated. The byte to be changed must be set to the return value of
 9  
  * {@link #getByteWithBitSet(byte, int, boolean)}.
 10  
  * 
 11  
  * @author Andreas Ergenzinger
 12  
  */
 13  0
 public final class BitManip {
 14  
 
 15  
     /**
 16  
      * Sets a single bit. If the <i>value</i> parameter is <code>true</code>, the bit will be set to <code>one</code>,
 17  
      * and to <code>zero</code> otherwise. All other bits will be left unchanged.
 18  
      * <p>
 19  
      * The bits are numbered in big-endian format, from 0 (LSB) to 7 (MSB).
 20  
      * <p>
 21  
      * <code>          
 22  
      * +---+---+---+---+---+---+---+---+<br>
 23  
      * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | bit number<br> 
 24  
      * +---+---+---+---+---+---+---+---+<br>
 25  
      * </code>
 26  
      * 
 27  
      * @param b the original byte value
 28  
      * @param bitNumber the big-endian position of the bit to be changed, from 0 to 7
 29  
      * @param value <code>true</code> for <i>1</i>, <code>false</code> for <i>0</i>
 30  
      * @return the edited byte value
 31  
      */
 32  
     public static final byte getByteWithBitSet (final byte b, final int bitNumber, final boolean value) {
 33  
 
 34  0
         int number = b;
 35  
 
 36  0
         if (value) {
 37  
             // make sure bit is set to true
 38  0
             int mask = 1;
 39  0
             mask <<= bitNumber;
 40  0
             number |= mask;
 41  0
         } else {
 42  0
             int mask = 1;
 43  0
             mask <<= bitNumber;
 44  0
             mask ^= 255;// flip bits
 45  0
             number &= mask;
 46  
         }
 47  
 
 48  0
         return (byte) number;
 49  
     }
 50  
 
 51  
     /**
 52  
      * Returns <code>true</code>, if the bit at the given position is set to <code>one</code> and <code>false</code> if
 53  
      * it is set to <code>zero</code> . The bits are numbered in big-endian format, from 0 (LSB) to 7 (MSB).
 54  
      * <p>
 55  
      * <code>
 56  
      * +---+---+---+---+---+---+---+---+<br>
 57  
      * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | bit number<br> 
 58  
      * +---+---+---+---+---+---+---+---+<br>
 59  
      * </code>
 60  
      * 
 61  
      * @param b the byte value in question
 62  
      * @param bitNumber the big-endian position of the bit to be changed, from 0 to 7
 63  
      * @return <code>true</code> if bit is set to <code>one</code>, else <code>false</code>
 64  
      */
 65  
     public static boolean getBit (final byte b, final int bitNumber) {
 66  0
         int number = b;
 67  0
         number >>>= bitNumber;
 68  0
         number &= 1;
 69  0
         if (number == 1) return true;
 70  0
         return false;
 71  
     }
 72  
 }