Binary, bit operation, and storage of basic data types in java

table of Contents

JDK's own base conversion

Bit operation

Integer type in java

Big Number BigInteger

Floating point type in java

​Exact decimal BigDecimal

Character and Boolean in java

Char type storage and calculation

Boolean storage


JDK's own base conversion

You can directly declare binary, octal, decimal, and hexadecimal in java

E.g:

Two-level system: int bin = 0b 1100010;

Octal: int oct = 0 142;

Decimal: int dec = 98;

Hexadecimal: int hex = 0x 62;

public static void main(String[] args) {
        int bin = 0b1100010;
        int oct = 0142;
        int dec = 98;
        int hex = 0x62;
        //前缀是数字0,不是字母o
        //英文字母b,x是不区分大小写的
        //在指定进制中使用规定的数码
        //底层存储都是二进制的形式
        //java默认使用十进制,输出显示都是十进制的形式
        System.out.println("2进制:" + bin);
        System.out.println("8进制:" + oct);
        System.out.println("10进制:" + dec);
        System.out.println("16进制:" + hex);

        //十进制转换成2,8,16进制输出到控制台
        int num = 98;
        System.out.println("98转换成2进制输出:"+Integer.toBinaryString(num));
        System.out.println("98转换成8进制输出:"+ Integer.toOctalString(num));
        System.out.println("98转换成10进制输出:"+Integer.toHexString(num));
}

 The output is as follows:

Integer.toString(int i,int radix) method, converted to a custom hexadecimal display, the range is 2~36 hexadecimals, the maximum range is 36 because the English alphabet has a total of 26 values ​​az

Plus the Arabic numerals 0-9, exactly 36.

 //使用Integer.toString(int n,int radix)方法任意进制的转换
        System.out.println("98转换成2进制输出:" + Integer.toString(num, 2));
        System.out.println("98转换成8进制输出:" + Integer.toString(num, 8));
        System.out.println("98转换成16进制输出:" + Integer.toString(num, 16));

        System.out.println("98转换成5进制输出:" + Integer.toString(num, 5));
        System.out.println("98转换成36进制输出:" + Integer.toString(num, 36));
        //超出了最大的36进制,还是默认进行10进制输出
        System.out.println("98转换成37进制输出:" + Integer.toString(num, 37));
        System.out.println("98转换成100进制输出:" + Integer.toString(num, 100));

The output is as follows:

Then the displayed results are all strings of type String, how to convert them back?

  • Method 1: Integer.parseInt(String s,int radix) method, return value type int
  • Method 2: Integer.valueof(String s,int radix) method, return value type Integer

Bit operation

Bit operation is to directly operate on the binary bits of the integer in the memory , logical operation or shift operation.

The binary number itself has the characteristics of logical operation.

There is a limit on the number of bits of data stored in the computer.

Bits and bytes:

Bit: The smallest unit of information, the unit is lowercase b.  

Byte (byte): Represents the smallest unit of information, the unit is uppercase B, 1 byte = 8 bits.

Number of machines:

Set the first bit of the data as the sign bit, 0 is positive and 1 is negative.

Features of machine numbers: Symbols are digitized, and the size of numbers is limited by the word length of the machine.

The form of the machine number: original code, inverse code, complement code

rule:

原码:10001111
反码:11110000
补码:11110001
正数反bai码、补码是其du本身。
负数反码为:原码符号zhi位不变,其他位全变dao。
负数补码为:原码从右边数第一个1右边的不变(包括第一个1),第一个1左边的全变,符号位不变。

 Complementary code calculation method: positive complement code = inverse code = original code, negative complement code = inverted code +1. 

 Bit arithmetic is to directly manipulate the binary digits of integers.

 

  • Bitwise AND operation: &

Definition: The two data participating in the operation are ANDed according to the binary digits. If both numbers are 1, the result is 1.

规则: 1&1 = 1,1&0=0,0&1=0,0&0=0

Example: 14&3 is 0000 1110 & 0000 0011 and the   result of 0000 0010 is the integer 2, so 14&3 = 2

Features: Clear specific bits and get specific bits.

Scenario: You can use num & 1 to determine the parity of the number num. If the binary mantissa is 1, it means that the number is odd. 

  • Bitwise OR operation: |

Definition: The two data participating in the operation are "OR" operated by binary bits. One of them is 1, and the result is 1.

Rules: 1|1 = 1, 1|0 = 1, 0|1=1, 0|0 =0

 Example: 14|3 is  0000 1110  |  0000 0011 and the result of 0000 1111  is the integer 15, so 14|3 = 15

Features: Set the specific bit to 1.

  • Bitwise XOR operation: ^

Definition: The two data participating in the operation are subjected to an "exclusive OR" operation based on binary digits. If the two numbers are not the same, the result is 1.

Rule: 1^1 = 0, 1^0 = 1, 0^1=1, 0^0 = 0

Example: 14|3 is  0000 1110  ^  0000 0011 and the result of 0000 1101  is the integer 13, so 14^3 = 13

Features: XOR with oneself gets 0, and XOR with a number continuously gets oneself.

  • Bitwise inversion operation: ~

Definition: Invert a binary number bit by bit, 0 changes to 1, 1 changes to 0 

Rules: ~1 = 0, ~0=1

Example: ~14 means ~0000 1110 gets 1111 0001 and the result is an integer -15, and the storage is a complement, so the original code is 10001111, so ~14 = 15

  • Left shift operation: <<

Definition: Shift a binary number to the left by the corresponding number of digits.

Rules: The sign bit remains unchanged, the low bit on the right is filled with 0, and the high bit on the left is discarded.

Example: 14<<1, that is, 0000 1110 <<1 gives 0001 1100 and the result is the integer 28, so 14<<1 = 28 

Features: m<<n is equivalent to m*2 to the nth power (when the highest bit is not 1)

  • Right shift operation: >>

Definition: Move a binary number to the right by the corresponding number of digits

Rules: the sign bit remains unchanged, the low bit on the right is discarded, the high bit on the left, positive numbers complement 0, negative numbers complement 1

Example: 14>>1 is 0000 1110 >>1 to get 0000 0111 and the result is the integer 7, so 14>>1 = 7

 -14 >> 2 means 1111 0010 >> 2 is 1111 1100 and the result is the integer -4, so -14 >> 2 =-4 

Features: m>>n is equivalent to m/2 to the nth power, (when the low bit is 1, the accuracy will be lost)

  • Unsigned right shift operation: >>>

Rule: Abandon the low bit on the right, and fill in the high bit on the left with 0

-14 Use 1 byte to get data representation

Example: -14 >>>2 means 1111 0010 >>>2 to get 0011 1100 and the result is the integer 60, so 14>>>2 = 60.

-14 Use 2 bytes to get data representation

Example: -14>>>2 is 1111 1111 1111 0010 >>>2 gets 0011 1111 1111 1100 and the result is an integer of 16380 

Integer type in java

 Big Number BigInteger

Can store theoretically unlimited integers [computer irrational limit]

Numerical storage is twos complement, big-endian mode storage

You can use different bases to perform basic operations and bit operations.

Commonly used methods:

//创建方法
BigInteger(String val) 十进制字符串表示形式转换为BigInteger
BigInteger(String val,int radix) 指定基数的字符串表示形式转换为BigInteger
static valueOf(long val) 返回其值等于指定long的值的BigInteger

//转字符串
toString(int radix)

//基本运算加减乘除
add(BigInteger val)        +
subtract(BigInteger val)   - 
multiply(BigInteger val)   *
divide(BigInteger val)     /

remainder(BigInteger val) %

divideAndRemainder(BigInteger val) 返回 / 和 % 的两个BigInteger的数组
compareTo(BigInteger val) 比较两个数的大小

 

 Floating point type in java

Fixed-point number: It is agreed that the decimal point of all numerical data is implicit in a certain fixed position.

Floating point number: The position of the decimal point of numerical data is not fixed

Exponential rule: Any real number can be obtained by an integer power of a fixed-point number * base number.

3.14159 = 0.314159*10 to the 1st power or 31.4159*10 to the 2nd power

 

 Single precision float (4 bytes) and double precision double (8 bytes)

float: 1 sign bit + 8 bit order code + 23 mantissa

double: 1 sign bit + 11 bit order code + 52 mantissa

 Exact decimal BigDecimal

 It is used to perform accurate calculations on decimals with more than 16 significant digits.

Numerical storage uses BigInteger + scale scale for storage to ensure accuracy. (Decimal scientific notation)

BigDecimal statement creation and basic operations.

       //创建BigDecimal
        BigDecimal b1 = new BigDecimal("0.1");
        BigDecimal b2 = new BigDecimal(0.1); //不推荐
        BigDecimal b3 = BigDecimal.valueOf(0.1);

        System.out.println(b1.toString());
        System.out.println(b2.toString());
        System.out.println(b3.toString());

 The output is as follows:

 

 // 除法运算,保留5位小数,四舍五入模式
  BigDecimal divide = b3.divide(BigDecimal.valueOf(3), new MathContext(5, RoundingMode.HALF_UP));
        System.out.println(divide);   //0.033333

Character and Boolean in java

Characters and character sets

Characters: A general term for various characters and symbols, referring to national characters, punctuation, etc...

Problem: Computers can only process numbers, and must convert text to numbers to process.

Character set: contains the font table, coded character set, and character code.

ASCII character set:

American Standard Code for Information Interchange: A coding system based on Latin characters.

Standard ASCII specifies a combination of 7-bit binary numbers to represent 128 kinds of character information, and the smallest unit byte of the computer is 8-bit---extended ASCII.

Problem: Different countries have different characters, and extended ASCII cannot be guaranteed.

Unnicode character set:

Unify the character encoding of all countries

Unicode, Universal Code: All characters in the world are assigned a unique number.

Unicode only has a font table and a coded character table, and there is no prescribed character code.

UTF-4,UTF-8,UTF-16,UTF-32.... 

UTF (UnicodeTransformationFormat) means Unicode conversion format.

Char type storage and calculation

char (2 bytes) uses Unicode character set, utf-16

It can only represent characters whose Unicode number is within 65536.

Char type addition and subtraction operation is to operate according to its Unicode number.

Uppercase: AZ: 65-90, lowercase: az: 97-122, numbers 0-9: 48-57

Boolean storage

boolean is used to represent true and false.

When individually defined: storage space is the same as int, 4 bytes 32 bits, a trade-off between storage space and execution efficiency.

If defined in an array: boolean will be compiled into a byte (1 byte 8 bits) type array.

 

So far, everyone is welcome to leave a message and discuss, if you like this article, just one click and three links!

Guess you like

Origin blog.csdn.net/qq_31905135/article/details/109175947