[Widget class] Convert a decimal number to binary/quaternary/octal/hexadecimal

First go directly to the complete code:

public class Test {
	
	public static void main(String[] args) {
		int num = 100;
		System.out.println(toBinary(num));
		System.out.println(toQuaternary(num));
		System.out.println(toOctal(num));
		System.out.println(toHexadecimal(num));
	}
	
	//Convert decimal to binary
	public static String toBinary(int num){
		return trans(num, 2, 1);
	}
	
	//Convert decimal to quaternary
	public static String toQuaternary(int num){
		return trans(num, 4, 2);
	}
	
	//Convert decimal to octal
	public static String toOctal(int num){
		return trans(num, 8, 3);
	}
	
	//Convert decimal to hexadecimal
	public static String toHexadecimal(int num){
		return trans(num, 16, 4);
	}
	
	/**
	 * Convert the decimal number num to base number
	 * @param num decimal number
	 * @param base base number
	 * @param offset right shift number
	 */
	public static String trans(int num, int base, int offset){
		
		if(num == 0){
			System.out.println("0's "+ base + "hex is 0");
			return "0's" + base + "hex is 0";
		}
		
		char[] arr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
		StringBuffer sb = new StringBuffer();
		while(num != 0){
			int temp = num&(base-1); //The bottom layer of the bit operation is the operation implemented by two binary numbers
			sb.append(arr[temp]);
			num = num >>> offset; // unsigned bit shift right
		}
		sb = sb.reverse(); //reverse
		return sb.toString();
	}

}

Summary of logical operations:

1. Logical operator : refers to a relationship, role: an expression used to connect two Boolean types

Including the following:

&: and, features: true on both sides, false on one side

| Or, feature: true if one side is true, false if both sides are false

! Not, features: non-true is false, non-false is true

^ XOR characteristics: the same is false, the difference is true Law: a number at the same time XOR this number twice, the result is still the same number. for encryption

&& Double and Features: Same as & , but with a little difference, discussed separately below.

|| Double or Features: Same as | , but with a few minor differences, discussed separately below.

<<: Left shift rule: The number of left shifts is the number of times the number is multiplied by 2 .

>>: right shift: rule: right shift a few bits is divided by 2 several times

Pay attention to the problem of left shift and right shift in the high position, which can be summed up in one sentence: a simple sentence: take whatever is the highest , take 0 if the high position is 0 , and take 1 if the high position is 1 ;

>>>: unsigned right shift: different from >> is the vacancy that appears in the high-order bit, no matter what the high-order bit is, it is complemented by 0 ;

2. The difference between & and && :

Common point: both can be used as operators of logical AND, representing logical AND,

The difference: && has a short-circuit function. If the first expression is false , the operation of the second expression does not need to be performed.

When the expressions on both sides of the & operator are not of type boolean , it means a bitwise AND operation. Eg: We usually use 0x0f to perform & operation with an integer to get the lower four bits;

3. The difference between | and || :

 | : No matter what the result of the operation on the left is, the right side participates in the operation.

 || : When the left side is true, the right side does not participate in the operation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472272&siteId=291194637