C-Language advanced-Signed and unsigned 02

Source: Internet
Author: User

Inside the computer, all of our information is made up of binary numbers.

The symbolic number of the table real method:
    • The number of symbols is in the form of a complement within the computer
    • A positive complement of positive digits
    • The complement of a negative number is reversed for its absolute value and then added one gets

For example 7 He is inside the computer is 1111 1001 is so get 7-"111-" 0000 0111-"1111 1000-" 1111 1001

The representation of an unsigned integer:
    • Using the original code to list real unsigned integers inside the computer
    • unsigned integers default to positive numbers
    • unsigned integers have no sign bit
For fixed-length unsigned integers:
    • Max+1=min
    • Min-1=max

The default is a signed type in C, and you can declare integers of unsigned types through unsigned, with special care that only integers can be declared as unsigned integers, and floating-point numbers do not have unsigned types

code Example 1:

#include <stdio.h>intMain () {Charc =-5;  Shorts =6; inti =-7; printf ("%d\n", (C &0x80) !=0 )); printf ("%d\n", ((S &0x8000) !=0 )); printf ("%d\n", ((I &0x80000000) !=0 )); return 0;}

This can be done with (bitwise arithmetic) to experiment with the highest bit is 0 or 1

code Example 2:

#include <stdio.h>intMain () {unsignedinti =5; intj =-Ten; if((i + j) >0) {printf ("i + j > 0\n"); }    Else{printf ("i + j <= 0\n"); }        return 0;}

This code should be carefully analyzed, -10+5 should be a negative number, but for wonder why will print i+j>0. Because when calculating unsigned and signed types, the computer converts the signed symbol to unsigned and then begins the calculation

Example Code 3:

#include <stdio.h>int  main () {     int0;          for (i=9; i>=0; i--)    {        printf ("i =%u\n", I);    }         return 0 ;}

This code incorrectly uses an unsigned type, causing the loop to not exit because the unsigned type is a large positive number after you are 0-1

Dfsaf

C-Language advanced-Signed and unsigned 02

Related Article

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.