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 relationship between negative computer representation and overflow

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what is the relationship between negative computer representation and spillover". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

§computer representation of 0x00 numbers

At school, it is said that numbers are represented in the form of complements in the computer. The definition of the complement is:

The complement and source code of a positive number are the same as the numerical value.

The highest bit of the original code is the symbol bit, and the other bits are values equal to the data.

The negative inverse code is that the symbol bit of the original code remains the same, flip the other bit, and then the inverse code + 1 is the complement.

Why is this so? This starts with the adder in the basic digital circuit.

Why does 0x01 say that?

The basic principle of the adder is to carry out logical operations according to bits, and carry to participate in the logical operations of the first bit of high school. However, it does not support subtraction. So this problem can be solved when negative numbers are stored in the form of complements. The following example: 4-2 of 1byte, that is, 4 + (- 2).

-2: 0b1000 0010 (original code), 0b1111 1101 (inverse code), 0b1111 1110 (complement) 4: 0b0000 0100 (original code), 0b0000 0100 (inverse code), 0b0000 0100 (complement)

The final calculation process is the addition of complement and complement.

0b1111 1110+0b0000 0100-0b0000 0010 = (+ 2)

The final symbol bit becomes positive because the carry flip is 0.

§0x02 has symbol number overflow

The width of the signed short type is 16bit. Its range is-32768mm 32767. Why does the maximum positive value + 1 overflow to-32768?

The complement of the largest positive number is 0x7fff in hexadecimal. After + 1, it does not overflow, but becomes 0x8000. The highest bit after rounding is 1, which is a negative number. What is the true value of 0x8000?

0x8000 (complement) minus 1 deducts the inverse code: 0x7fff flipped bit by bit to mention the original code: 0x8000

So what should the value of 0x8000 be? It seems to be 0, the highest bit is 1, which means it is negative, and all the other bits are 0, which is-0. -0# 1 should be 1. What's the reality?

0x8001 (complement)-1 inverse code: 0x8000 bitwise flip: 0xffff

The original code value of 0xffff is-32767. 0x8000 should be it-1, so the value of the 0x8000 complement should be-32768.

This is the end of the content of "what is the relationship between negative computer representation and overflow". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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