How to calculate the range of binary numbers

Simply put, the computer is a transistor, the circuit board assembled to the electronic device, whether the graphic image is rendered, the remote network share, or large data computation, signal processing ultimately are 0 and 1. Metadata information storage and logical calculations, can only be 0 and 1, but physically they are different in expression in the media is not the same as the transistor off energization, the CPU of the low and high, charge left direction of the disk. Clear physical manifestations of 0 and 1, the base 2 is set, carry rule is "every binary a" borrow rule is "when the two take a" so called binary.

Return to the topic, how to calculate the range of binary numbers it? For example, Java an int data type is 32, then the int range that can be expressed is how much?

Imagine eight circuits, each circuit has two states low and high. The mathematical permutations, 8 is multiplied by 2, i.e., 2 ^ 8, can represent 256 different signals. Suppose representing the interval from 0 to 255, that is, the maximum number of 2 ^ 8-1 = 256-1 = 255, the reason is because the first subtracting 1 is used to represent the number of 0, it is possible to represent the total number minus 1 equals the maximum number , then the maximum number of circuit 32 can be represented by (2 ^ 32-1) = 4,294,967,295. Usually said 32-bit machine, it is capable of handling 32-bit word length of the signal circuit.

How to represent negative it? The above circuit 8, the leftmost one indicates positive or negative, 0 for positive, 1 for negative, this bit is the sign bit represented by numerical computation does not participate. 8 circuit, only part of the actual value of 7, then the maximum value that is represented by binary 01111111 i.e. 127.

You can also change species thought to be appreciated that numerical portion 7 has, according to the arrangement combinatorics, 7 multiplied by 2, i.e., 2 ^ 7, 128 kinds of signals can be represented. Plus the first bit of the symbol bit 1 or 0 can be expressed 128 x 2 = 256 types of signals. Including positive 128, negative 128, i.e., the range of 0 to 127, -128 to -1, represents the range of positive and negative points due to change of -128 to 127.

Binary integer ultimately appeared in two's complement form. Complement positive numbers with the original code, anti-code is the same, but negative complement is the result of the anti-code plus 1. Such subtraction may be used so that the adder is implemented, the sign bit is also involved in computing. Note that the negative sign bit of the original code, the inverted, complement is always 1, and only part of the value involved in the original code to the inverse code conversion operator to the original code of complement.

Back to the original question, Java is int data type is 32, then the int range that can be expressed is how much?

The number of ranges of values ​​can be represented by the portion 2 ^ 31, the sign bit count of 0 or 1, i.e., a positive number of 0 to 2 ^ 31-1, negative range -2 ^ 31 to -1, -2 total ^ 31 to 2 ^ 31-1.


Reference material

"A highly efficient code: Java Development Manual"

Guess you like

Origin www.cnblogs.com/yueshutong/p/12011086.html