A piece of code (1) -- unsigned and signed, unsignedsigned

Source: Internet
Author: User

A piece of code (1) -- unsigned and signed, unsignedsigned
Symptom:

Let's take a look at a piece of code:


The output result of this Code is:

-84

4294967264

Analysis:
xiaoqiang@dev:~/cpp$ g++ -g c212.cc -o tempxiaoqiang@dev:~/cpp$ lsc143.cc  c144.cc  c212.cc  temp
One more temp file is displayed.
xiaoqiang@dev:~/cpp$ gdb tempGNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04Copyright (C) 2012 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later (gdb) b 6Breakpoint 1 at 0x4007ea: file c212.cc, line 6.

3 int main(){4 unsigned u = 10;5 int i = -42;6 std::cout << i + i << std::endl;7 std::cout << u + i << std::endl;8 return 0;9 }(gdb) Go to the gdb debugging mode to check whether
(gdb) b 6Breakpoint 1 at 0x4007ea: file c212.cc, line 6.
When you start running, you will find that the program stops at 6th rows.
(gdb) rStarting program: /home/xiaoqiang/cpp/temp warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000Breakpoint 1, main () at c212.cc:66               std::cout << i + i << std::endl;(gdb) 
Let's see what the binary value of I is.
(gdb) print /t i$1 = 11111111111111111111111111010110
When using unsigned int and int type variable operations, the int type is first converted to the (AS) unsigned int type. The above two values are added in the memory as follows: 11111111 11111111 11111111 01100000 hexadecimal format: FFFFFFE0 in decimal format: 4294967264 exercise: Check the lower part of the code and guess the final output result:
xiaoqiang@dev:~/cpp$ gdb tempGNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04Copyright (C) 2012 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later Let's take a look at the binary representation of u1 and u2.
Breakpoint 1, main () at c213.cc:55               std::cout << u1 - u2 << std::endl;(gdb) next326               std::cout << u2 - u1 << std::endl;(gdb) next42949672647               return 0;(gdb) print /t u2$2 = 1010(gdb) print /t u1$3 = 101010(gdb) 
The difference is 11111111 111111111 11111111 11100000. If the value is unsigned int, the value is 4294967264. If the value is int (Signed), the value is-32. related Knowledge: 1. What is GDBGDB? It is a powerful UNIX program debugging tool released by the GNU open-source organization. Maybe you prefer the GUI, such as VC, BCB, and other IDE debugging methods. But if you are running software on a UNIX platform, you will find that the GDB debugging tool has more powerful functions than the visual debugger of VC and BCB. This is the so-called "have an inch, have a small size and have a short size.
2. When compiling the basic commands of GDB, the parameter-g must be added. For example, g ++-g temp. cpp-o temp. the executable files can be generated through Gcc compilation before Gdb can be used for debugging. Go to the gdb interface: gdb temp. prompt to (gdb) (1) view the file by typing "l" (list) in Gdb (2) to set a breakpoint, you only need to add the corresponding line number after "B" (this is the most common method, and there are other ways to set the breakpoint ). As shown in the following figure: (gdb) the code B 6 is paused before it is run to the fifth line (the fifth line is not run ). (3) view the breakpoint (Gdb) info B (4) run the code. Gdb runs the code from the first line by default. You can type "r" (run) you can (if you want to specify a row in the program to start running, you can add a row number after r ). (5) view the variable value and view the variable value at the breakpoint. In Gdb, you only need to type "p" + variable value, as shown in the following figure: (Gdb) p nGdb adds the "$ N" mark before the corresponding value when displaying the variable value, it is the reference mark of the current variable value, so if you want to reference this variable again later, you can directly write "$ N" without the need to write lengthy variable names.
(6) One-step operation
Using the command "n" (next) or "s" (step), the difference between them is: if a function is called, "s" will enter the function, and "n" will not enter the function. Therefore, "s" is similar to "step in" and "n" in tools such as VC ". (7) run the "c" (continue) command to resume the program running ). in Gdb, the running status of a program is "run", "pause", and "stop". The "pause" status indicates that the program encounters a breakpoint or an observation point, the program stops running temporarily. At this time, the function address, function parameters, and local variables in the function are pushed into the Stack. In this status, you can view the variable values and other attributes of the function. However, after the function is in the "STOPPED" status, the "stack" will be automatically revoked, and it will not be able to view various information.
(8) view data print variable view variable print * array @ len view array (array is an array pointer, len is the data length required) You can add parameters to set the output format: /x displays variables in hexadecimal format. /D: Display variables in decimal format.
/U displays unsigned integers in hexadecimal format.
/O display variables in octal format.
/T display variables in binary format.
/A displays variables in hexadecimal format.
/C display variables in character format.
/F display variables in floating point format.







Conversion of unsigned and signed data in C Language

First, let's talk about the concept of the original code complement anti-code.
The positive integers are the same as the original complement codes.

The negative number is the inverse of the original code except the symbol bit.
The complement of a negative number is to remove all the original codes except the symbol bit, and then add 1 at the end.

Assume that a byte integer signed int8 x =-5 exists.
The source code of x is 1000 0101 (the highest bit is sign bit 1, indicating negative 0, indicating positive)
X's reverse Code 1111 1010
X completion Code 1111 1011

The-5 in the stored computing is not 10000101 that we can better understand, but the-5 complement Code 1111 1011.

Answer your question now:
Unsigned int e, f = 12345;
F is unsigned, so the original code of f is the same: 0000 0100 1101 0011
So c is equal to 12345

Signed int c, d =-15;
D is a negative number, so the original code of d is 1000 0000 0000 1111
D's complement is 1111 1111 1111 0001

C = f = 0000 0100 1101 0011 convert to decimal to 12345
E = d = 1111 1111 1111 0001 because e is an unsigned integer
1111 1111 1111 0001 is treated as a positive integer, and the process of converting the complement code into the original code does not exist.
Therefore, the e you see is not equal to-15 (in decimal format), but 65521 (in decimal format)

Summary: The key to this problem is that we see the original code on the computer screen, and the stored computation is the supplementary code. Positive Integers have the same source code, while negative integers have different source codes,
If you understand this concept, it will be easy to understand.

What is the role of unsigned and signed in C language?

It mainly refers to the value range.

Unsigned and signed are in one type. unsigned indicates an integer in the range. signed generally ranges from negative to positive.

Do you have any questions? HI, let's talk about it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.