Bit-level unpacking of floating-point data : Binary Bit « Language Basics « Java






Bit-level unpacking of floating-point data

  
/*
Java Programming for Engineers
Julio Sanchez
Maria P. Canton


ISBN: 0849308100
Publisher: CRC Press
*/

// Java for Engineers
//Filename: BitOps
//Reference: Chapter 24
//Description:
//         Bit-level unpacking of floating-pioint data
//Requires:
//         Keyin class in current directory


class BitOps {
  public static void main(String[] args) {
    // Definition of bit field masks for double
    final long SIGN = 0x8000000000000000L;
    final long EXPN = 0x7ff0000000000000L;
    final long SGNF = 0x000fffffffffffffL;
    final long BIT1 = 0x8000000000000000L;
    // Storage for bit fields
    long s; // Sign
    long e; // Exponent field
    long m; // Significand (mantissa) field
    String eS; // For conversions

    double num;
    long binVal;
    long t;

    // Get user input
    num = 3.4d;
    binVal = Double.doubleToRawLongBits(num);

    // Display hex bits
    System.out.println("As long = " + Long.toHexString(binVal));

    // Display bit fields of double format
    s = binVal & SIGN;
    if (s != 0)
      System.out.println("Sign = -");
    else
      System.out.println("Sign = +");

    // Mask out exponent field
    e = (binVal & EXPN);
    eS = Long.toHexString(e);
    System.out.println("Exponent = " + eS);

    // Mask out significand field
    m = (binVal & SGNF);
    eS = Long.toHexString(m);
    System.out.println("Significand = " + eS);

    System.out.println("\nFields in binary");
    if (s != 0)
      System.out.println("Sign bit = 1");
    else
      System.out.println("Sign bit = 0");

    // Display binary exponent
    // Eliminate sign bit
    e = e << 1;
    System.out.print("Exponent = ");
    for (int k = 0; k < 11; k++) {
      t = e & BIT1;
      // System.out.println(Long.toHexString(t));
      if (t != 0)
        System.out.print("1");
      else
        System.out.print("0");
      e = e << 1;
    }
    System.out.println("\n           |-11 bits-|");
    // Display binary significand
    // Eliminate exponent and sign bits
    m = m << 12;
    System.out.print("Significand = 1.");
    for (int j = 0; j < 51; j++) {
      t = m & BIT1;
      if (t != 0)
        System.out.print("1");
      else
        System.out.print("0");
      m = m << 1;
    }
    System.out.println("\n              ^ |");
    System.out.println("implicit bit -| | ----- 52 bits -->");
  }
}



           
         
    
  








Related examples in the same category

1. Utility for byte swapping of all java data types
2.Bitwise DemoBitwise Demo
3.Binary Digits
4.Using the bitwise operatorsUsing the bitwise operators
5.Bitwise complement (~): inverts ones and zeroes in a number
6.Convert a number to negative and back
7.Bitwise AND (&)
8.Bitwise OR (|)
9.Bitwise XOR (^)
10.Left shift (<<)
11.Performing Bitwise Operations on a Bit Vector
12.Converting Between a BitSet and a Byte Array
13.Returns a byte array of at least length 1
14.Shift to the left
15.Signed shift to the right
16.Unsigned shift to the right
17.Gets the a single bit of the target.
18.Returns 16 bits from the long number.
19.Sets a specific bit of an int.
20.Fuses the lower 16 bits of two ints.
21.Class to represent unsigned 32-bit numbers.
22.A list of bits.
23.Gets the specified bit (0-31) from the integer argument.
24.Sets the specified bit (0-31) in the integer argument.
25.Clears a range of bits in the specified integer.