Deep jvm Kernel-Principle, Diagnostics and Optimization-1. Initial jvm

Keywords: Programming Java jvm

1. Initial jvm

  1. For complements, briefly describe the benefits of complements.The complement given to -99, -105, 205 integers is calculated.

     A: Briefly describe the benefits of the complement:
     In people's computing concepts, there is no positive or negative division between zero and the treatment of uniform zero
     Unify additions and subtractions without adding subtractor operations
     	The positive binary complement equals itself, and the negative binary complement equals the negative + 1
     -99: 
     Source: 11100011
     Reverse code: 10011100
     Complement code: 10011101
     Others I'll just give the complement:
     -105: 10010111
     205: 00000000 11001101
    
  2. For floating point numbers, the single-precision floating point values of 11000001000100000000000000000000000 are calculated according to IEEE745, and the calculation process is given.

     1 Symbol bit 10000010 value is 3000100000000000000000 value is 1.001 
     = -1 * (2^3)*(2^0 + 2^-3)
     = -8*(1+8/1)
     = -8 -1
     = -9
    

    100.2 to IEEE745 binary disassembly analysis:

     1. Decimal to binary (32 bits), integer part divided by 2, decimal part rounded by 2.
     	 100.2 = 1100100.0011001100110011001100110
     2. Exponential formatting (keep one non-zero integer)
     	 1.10010000110011001100110110110110*2^6 (Integer bit is hidden extra bit)
     3. Calculate 8-bit index
     	 6+127=133=10000101 (6 is the exponent for step 2)
     4. Obtain IEEE745 binary representation
     		 0      10000101  10010000110011001100110
     	 Sign Bit Index Tail
    

    IEEE 754

    Source Code, Inverse Code, Complement Code

    Base type (assignment modifications between metatype variables do not affect each other)

    If the object references the same instance, assignment modifications between objects affect each other

  3. Write a Java program that converts 100.2 to IEEE745 binary representation, giving the program and results.

     Results: 010000110010000110011001100110
     //Program (lazy) method one:
     ```
     public static void main(String[] args) {
     		String value=convert(100.2f);
     		System.out.println(value);
     	}
    
     	public static String convert(float num) {
     		int intVal = Float.floatToIntBits(num);
     		return intVal > 0 ? "0" + Integer.toBinaryString(intVal) : Integer
     				.toBinaryString(intVal);
     	}
     ```
    

    (Not lazy) Method 2:

     ```
     package com.tencent.tubemq.example;
    
     import java.util.ArrayList;
     import java.util.HashSet;
     import java.util.List;
     import java.util.Set;
    
     /**
      * description:
      *
      * @author: dawn.he QQ:       905845006
      * @email: dawn.he@cloudwise.com
      * @email: 905845006@qq.com
      * @date: 2019/9/20    3:15 PM
      */
     public class DeadLockDemo {
     	private static String A = "A";
     	private static String B = "B";
    
     	static class Value {
     		int val;
     	}
    
     	static class Test {
     		public static void main(String[] args) throws InterruptedException {
     			new DeadLockDemo().n2(3,3);
     			System.out.println(new DeadLockDemo().floatToIEEE754(100.2f));
     		}
     	}
    
     	/**
     	 * thread deadlock
     	 */
     	private void deadLock() {
     		Thread t1 = new Thread(new Runnable() {
     			@Override
     			public void run() {
     				synchronized (A) {
     					try {
     						Thread.currentThread().sleep(2000);
     					} catch (InterruptedException e) {
     						e.printStackTrace();
     					}
    
     					synchronized (B) {
     						System.out.println("1");
     					}
     				}
     			}
     		});
    
     		Thread t2 = new Thread(new Runnable() {
     			@Override
     			public void run() {
     				synchronized (B) {
     					synchronized (A) {
     						System.out.println("2");
     					}
     				}
     			}
     		});
    
     		t1.start();
     		t2.start();
     	}
    
     	/**
     	 * Single Precision Floating Point to IEEE745
     	 * @param strf = 100.2f
     	 * @return String 1-100100-0011001100110011001100110
     	 * @author Monkey
     	 * */
     	/**
     	 * Get IEEE754 storage format for float
     	 */
     	public String floatToIEEE754(float value) {
     		//Symbol bits
     		String sflag = value > 0 ? "0" : "1";
    
     		//Integer part
     		int fz = (int) Math.floor(value);
     		//Integer Partial Binary
     		String fzb = Integer.toBinaryString(fz);
     		//Decimal part, format: 0.02
     		String valueStr = String.valueOf(value);
     		String fxStr = "0" + valueStr.substring(valueStr.indexOf("."));
     		float fx = Float.parseFloat(fxStr);
     		//Decimal Partial Binary
     		String fxb = toBin(fx);
    
     		//Exponential bits
     		String e = Integer.toBinaryString(127 + fzb.length() - 1);
     		//mantissa
     		String m = fzb.substring(1) + fxb;
    
     		String result = sflag + e + m;
    
     		while (result.length() < 32) {
     			result += "0";
     		}
     		if (result.length() > 32) {
     			result = result.substring(0, 32);
     		}
     		return result;
     	}
    
     	private String toBin(float f) {
     		List<Integer> list = new ArrayList<Integer>();
     		Set<Float> set = new HashSet<Float>();
     		int MAX = 24; // Up to 8 bits
    
     		int bits = 0;
     		while (true) {
     			f = calc(f, set, list);
     			bits++;
     			if (f == -1 || bits >= MAX)
     				break;
     		}
     		String result = "";
     		for (Integer i : list) {
     			result += i;
     		}
     		return result;
     	}
     	/*Decimal decimal to binary, mainly the decimal part multiplied by 2, integer part from left to right after the decimal point, until 0 after the decimal point.*/
     	private float calc(float f, Set<Float> set, List<Integer> list) {
     		if (f == 0 || set.contains(f))
     			return -1;
     		float t = f * 2;
     		if (t >= 1) {
     			list.add(1);
     			return t - 1;
     		} else {
     			list.add(0);
     			return t;
     		}
     	}
    
     	/**j n-th power of */
     	public void n2(int j, int n) {
     		if (j == 0) {
     			j = 2;
     		}
    
     		int k = 0;
     		for (int i = 0; i < n; i++) {
    
     			if (k == 0) {
     				k = 1 * j;
     			} else {
     				k = k * j;
     			}
     		}
     		System.out.println(k);
     	}
     }
     ```
    

Posted by Ben Cleary on Sat, 21 Sep 2019 17:51:03 -0700