Analysis of the value range of char and unsigned char

1. Instance analysis:

#include <stdio.h>

typedef struct CHAR_Node_
{
    
    
    unsigned char ch_a;
    char          ch_b;
}CHAR_Node;

int main()
{
    
    
    CHAR_Node Test;
    int  i = 0;
    printf("a: \n");
    for(i = 0 ; i <= 255; i++)
    {
    
    
        printf("%d ", Test.ch_a++);
    }
    printf("\n");

    printf("b: \n");
    for(i = 0 ; i <= 255; i++)
    {
    
    
        printf("%d ", Test.ch_b++);
    }
    printf("\n");

    return 0;
}

2. Print

10 hex

Insert picture description here

Hexadecimal:

Insert picture description here

Binary

After searching for a long time, there was no binary print, and finally found a way to print the binary binary print link source author in the powerful CSDN blog

#include <stdio.h>


void bin(int n)
{
    
    
    if(n)
    {
    
    
        bin(n/2);
    }
    else
        return;
    printf("%d",n%2);
}


typedef struct CHAR_Node_
{
    
    
    unsigned char ch_a;
    char          ch_b;
}CHAR_Node;


int main()
{
    
    
    CHAR_Node Test;
    int  i = 0;
    printf("a: \n");
    for(i = 0 ; i <= 255; i++)
    {
    
    
        //printf("%b ", Test.ch_a++);
        printf("<%d: |", Test.ch_a);
        bin(Test.ch_a++);
        printf("|> ");
    }
    printf("\n");


    printf("b: \n");
    for(i = 0 ; i <= 255; i++)
    {
    
    
        printf("(%d:",Test.ch_b);
        bin(Test.ch_b++);
        printf(") ");
        //printf("%b ", Test.ch_b++);
    }
    printf("\n");


    return 0;
}
unsigned char:

Insert picture description here
char:
Insert picture description here
-1<—>11111111
-2<—>11111110
-3<—>11111101

-126<—>10000010
-127<—>10000001
-128<—>10000000 在这里
127<—>01111111
126<—>01111110
125<—>01111101

003<—>00000011
002<—>00000010
001<—>00000001
000<—>00000000

Specific ( 8-bit binary 10000000 in the list memory of 8-bit signed numbers represents -128 ):
-1<—>11111111
-2<—>11111110
-3
<—>11111101 -4<—>11111100
-5<—>11111011
-6<—>11111010
-7<—>11111001
-8<—>11111000
-9<—>11110111
-10<—>11110110
-11<—>11110101
-12<—>11110100
-13<—>11110011
-14 <—>11110010
-15<—>11110001
-16<—>11110000
-17<—>11101111
-18<—>11101110
-19<—>11101101
-20<—>11101100
-21<—>11101011
-22<— >11101010
-23<—>11101001
-24<—>11101000
-25<—>11100111
-26<—>11100110
-27<—>11100101
-28<—>11100100
-29<—>11100011
-30<—>11100010
-31<—>11100001
-32<—>11100000
-33<—>11011111
-34<—>11011110
-35<—>11011101
-36<—>11011100
-37<—>11011011
-38<—>11011010
-39<—>11011001
-40<—>11011000
-41<—>11010111
-42<—>11010110
-43<—>11010101
-44<—>11010100
-45<—>11010011
-46<—>11010010
-47<—>11010001
-48<—>11010000
-49<—>11001111
-50<—>11001110
-51<—>11001101
-52<—>11001100
-53<—>11001011
-54<—>11001010
-55<—>11001001
-56<—>11001000
-57<—>11000111
-58<—>11000110
-59<—>11000101
-60<—>11000100
-61<—>11000011
-62<—>11000010
-63<—>11000001
-64<—>11000000
-65<—>10111111
-66<—>10111110
-67<—>10111101
-68<—>10111100
-69<—>10111011
-70<—>10111010
-71<—>10111001
-72<—>10111000
-73<—>10110111
-74<—>10110110
-75<—>10110101
-76<—>10110100
-77<—>10110011
-78<—>10110010
-79<—>10110001
-80<—>10110000
-81<—>10101111
-82<—>10101110
-83<—>10101101
-84<—>10101100
-85<—>10101011
-86<—>10101010
-87<—>10101001
-88<—>10101000
-89<—>10100111
-90<—>10100110
-91<—>10100101
-92<—>10100100
-93<—>10100011
-94<—>10100010
-95<—>10100001
-96<—>10100000
-97<—>10011111
-98<—>10011110
-99<—>10011101
-100<—>10011100
-101<—>10011011
-102<—>10011010
-103<—>10011001
-104<—>10011000
-105<—>10010111
-106<—>10010110
-107<—>10010101
-108<—>10010100
-109<—>10010011
-110<—>10010010
-111<—>10010001
-112<—>10010000
-113<—>10001111
-114<—>10001110
-115<—>10001101
-116<—>10001100
-117<—>10001011
-118<—>10001010
-119<—>10001001
-120<—>10001000
-121<—>10000111
-122<—>10000110
-123<—>10000101
-124<—>10000100
-125<—>10000011
-126<—>10000010
-127<—>10000001
-128<—>10000000 在这里
127<—>01111111

126<—>01111110
125<—>01111101
124<—>01111100
123<—>01111011
122<—>01111010
121<—>01111001
120<—>01111000
119<—>01110111
118<—>01110110
117<—>01110101
116<—>01110100
115<—>01110011
114<—>01110010
113<—>01110001
112<—>01110000
111<—>01101111
110<—>01101110
109<—>01101101
108<—>01101100
107<—>01101011
106<—>01101010
105<—>01101001
104<—>01101000
103<—>01100111
102<—>01100110
101<—>01100101
100<—>01100100
099<—>01100011
098<—>01100010
097<—>01100001
096<—>01100000
095<—>01011111
094<—>01011110
093<—>01011101
092<—>01011100
091<—>01011011
090<—>01011010
089<—>01011001
088<—>01011000
087<—>01010111
086<—>01010110
085<—>01010101
084<—>01010100
083<—>01010011
082<—>01010010
081<—>01010001
080<—>01010000
079<—>01001111
078<—>01001110
077<—>01001101
076<—>01001100
075<—>01001011
074<—>01001010
073<—>01001001
072<—>01001000
071<—>01000111
070<—>01000110
069<—>01000101
068<—>01000100
067<—>01000011
066<—>01000010
065<—>01000001
064<—>01000000
063<—>00111111
062<—>00111110
061<—>00111101
060<—>00111100
059<—>00111011
058<—>00111010
057<—>00111001
056<—>00111000
055<—>00110111
054<—>00110110
053<—>00110101
052<—>00110100
051<—>00110011
050<—>00110010
049<—>00110001
048<—>00110000
047<—>00101111
046<—>00101110
045<—>00101101
044<—>00101100
043<—>00101011
042<—>00101010
041<—>00101001
040<—>00101000
039<—>00100111
038<—>00100110
037<—>00100101
036<—>00100100
035<—>00100011
034<—>00100010
033<—>00100001
032<—>00100000
031<—>00011111
030<—>00011110
029<—>00011101
028<—>00011100
027<—>00011011
026<—>00011010
025<—>00011001
024<—>00011000
023<—>00010111
022<—>00010110
021<—>00010101
020<—>00010100
019<—>00010011
018<—>00010010
017<—>00010001
016<—>00010000
015<—>00001111
014<—>00001110
013<—>00001101
012<—>00001100
011<—>00001011
010<—>00001010
009<—>00001001
008<—>00001000
007<—>00000111
006<—>00000110
005<—>00000101
004<—>00000100
003<—>00000011
002<—>00000010
001<—>00000001
000<—>00000000

3. Memory analysis:

Insert picture description here

4. Storage of negative numbers in C language

Negative numbers in C language are stored in the form of complement.
Example: negative number -1, (here, assuming 8-bit binary representation)
the original code corresponding to the positive number: 0000 0001;
negate: 1111 1110;
add 1: 1111 1111;
finally, -1 is in the form of 1111 1111 Stored.
Theoretical knowledge
There are three ways to express the number of symbols in computers, namely, original code, inverse code and complement. The three representation methods have two parts: the sign bit and the value bit. The sign bit uses 0 to indicate "positive" and 1 to indicate "negative". For the value bit, the three representation methods are different.
The complement of a positive integer is its binary representation, which is the same as the original code; to find the complement of a negative integer, all bits of its corresponding positive binary representation are inverted (including the sign bit, 0 becomes 1, 1 becomes 0) and then 1 is added;
Reference negative storage

Guess you like

Origin blog.csdn.net/Interesting1024/article/details/109389437