Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Python and c++/c/java in storing negative numbers?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "what is the difference between Python and c++/c/java in the storage of negative numbers?" interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between Python and c++/c/java in storing negative numbers?"

Integers in Python are stored in complementary form

In Python, bin is a negative number (decimal representation), and the output is the binary representation of its original code with a minus sign, which is easy to view.

Bin a negative number (hexadecimal representation) in Python, and the output is the corresponding binary representation.

1. The way negative numbers are stored in python

Example

A = bin (- 3) print (a) a = bin (3) print (a) b = bin (- 3 & 0xffffffff) print (b) c = bin (0xfffffffd) print (c) / output / /-0b11//0b11//0b11111111111111111111111111111101//0b11111111111111111111111111111101

In other words:

Integers in Python are stored in complementary form

In Python, bin is a negative number (decimal representation), and the output is the binary representation of its original code with a minus sign, which is easy to view.

Bin a negative number (hexadecimal representation) in Python, and the output is the corresponding binary representation.

So in order to get the complement of a negative number (decimal representation), you need to manually bitwise and operate it with the hexadecimal number 0xfffffffd, and the result is also a hexadecimal number, and then give it to bin () for output, what you want is the complement representation.

2. But in c/c++/java, negative numbers are stored in the form of complement. "computer principle" shows that the computer uses the complement of 2 (Two's Complement) to represent negative numbers.

3. This leads to the need to operate the negative number and 0xffffffff in Python to remove the negative sign in front of the negative number, which can be understood as something more than 32 bits will not be considered. The specific steps for carrying out and operation are: if it is a positive number, directly with; if it is a negative number, first remove the front minus sign, then reverse, add 1, and then operate. So as to get the complement of negative number.

So we also have to truncate the output a, but we can't simply and rudely directly & 0xffffffff, because doing so-1 plus 1 is right, and the result is positive, but if the original result is negative, there will be a strange result again. Finally, the real solution is as follows:

Example

Def getSum (0xffffffff b): while baccalaure0: ta = an a = a ^ b = ((ta&b) 31 if hibit==1: return-((~ a) + 1) & 0xffffffff) else: return a&0xffffffff

Its principle is to judge whether it is a negative number by the 32-bit sign bit first, then to add 1 and then truncate if it is a negative number, and finally to add a negative sign, and a positive number to truncate directly. As a result, the supposedly simple and easy version of Python turned out to be like this, which is weird.

4. So you can check your own written sword pointing to Offer: the solution of the number of 1s in the binary. The difference between C++ programs and python programs (the difference between negative complements).

And in this problem, we should also pay attention to the number of subtractive operations and operations.

5. Solve the number of 1s in the binary and write it in python. That's how it works.

Example

Class Solution: def NumberOf1 (self, n): # write code here if n

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report