search
HomeJavajavaTutorialwhat is java int

what is java int

May 30, 2019 am 09:20 AM

what is java int

In Java, int is represented by 32 bits, and long is represented by 64 bits. These two basic types are integers and have no other representation methods. How do they express themselves internally? It is represented by two's complement code. Since we often encounter Java's bit operations in solving problems, Java's bit operations can only be used for int and long types. So we have reason to analyze how Java is represented internally by binary. Here we only take int as an example, long is the same.

1. The range of int

int is represented by 32 bits internally in Java, and the highest bit represents the symbol, so the number that can really be represented is 31 Bit. The value range of Java's int type is -2^31~2^31-1.

Why is the range like this? This involves the binary storage method.

In Java, two's complement is used to store int. For a positive number, its complement is itself. For example, let's take 4 bits as an example. For positive numbers 1-7, they are 0001,0010, respectively. . . , 0111, a total of 7 numbers, which is 2^3-1.

For 0, you can see that the problem is coming. It can be -0 or 0, both are 0. Decide how to represent 0? The law of complement codes stipulates that 0 is considered to be 0, so 0 is represented by 0000.

At this time, it is necessary to express negative numbers. We believe that the complement representation of a negative number is the inversion of its absolute value (inversion along with the sign bit) plus 1. For example, for -5, its absolute value is 5, which is expressed as 0101. If it is inverted, it is 1010, and plus one is 1011. This is the complement representation of -5.

At this time, 1000 can be used to represent -8, so the range of negative numbers is -2^31, which also explains the value range of int. The same is true for 64-bit long.

2. Java’s bit operations

How is Java’s bit operation performed? Remember, a shift operation moves the entire number to the left or right as a whole. If it is shifted to the left, the low bit is filled with 0. If it is moved to the right, there are two situations. If it is 0 and a positive number, the high bits are filled with 0. If it is a negative number, the high bit is filled with 1. For example, -5 is 1011, and shifting one position to the right is 1101, which is -3.

There is also a bit operation. If it is a bitwise operation, remember that the sign bit also needs to be involved!

3. The relationship between right shift and division

For positive numbers (of course 0 is also the case), shifting one position to the right is equal to the result of dividing by 2, but for positive numbers Not for negative numbers (the result of dividing by 2 -1 to be exact)! For example, -5/2 results in -2, and -5>>1 results in -3.

4. The relationship between positive and negative signs and integer division and remainder operations

Positive numbers/Positive numbers, round down

Negative numbers/ Negative numbers are equal to dividing their absolute values

Positive/negative numbers and negative/positive numbers are equal to dividing their absolute values ​​multiplied by the negative sign.

As for the results of %, the absolute value of the result is the same as the modulus of their absolute value, but the sign is determined by the first number.

5. Why use complement code?

(1) As mentioned before, in order to reasonably represent 0 and -0;

(2) You can express one more negative number at a negative number;

(3) Using complement code, the sign bit and other bits can be processed uniformly; at the same time, subtraction can also be processed as addition. In addition, when two numbers expressed in two's complement are added, if there is a carry in the highest bit (sign bit), the carry is discarded.

In fact, a big reason is to facilitate calculation of addition and subtraction. When using the two's complement system for operations, the sign bits can be added together for operation. In bit operations, if there is a carry in the sign bit, it can be discarded because it has exceeded the bit range. There's nothing special about positive numbers and their addition. The examples below are all 8 bits. For example, for 7 7, which is 00000111 00000111, the result is 00001110, which is 14.

For subtraction, for example, 9-4, we think it is 9 (-4). First, invert 4, which is 00000100, and add one to get the complement of -4, which represents 11111100, and then add it. , that is,

00001001(9)

11111100(-4)

? 100000101

The carry of the last sign bit is 1, because it has exceeded has exceeded the bit range, so discard it directly and get 5 directly. So this is the realization of subtraction changing to addition.

The above is the detailed content of what is java int. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool