What did the negative number do?

Source: Internet
Author: User

1. How does 1 and-1 represent in memory?

2. How does one add a negative number to the memory?

3. Thinking Caused by an exercise.


First introduce a small tool to convert a char into a binary representation.

static void n2(unsigned char c) {    int i;    for (i = 0; i < 8; i++) {        if (128 & c)            putchar(‘1‘);        else            putchar(‘0‘);        c <<= 1;    }       printf("\n");}

Usage:

n2(1);n2(-1);

Output:

 1:00000001-1:11111111

Print several more numbers.

 3:00000011-3:11111101 4:00000100-4:11111100

New discoveries:

The binary value of 1 is 00000001, And the inverse value is 11111110, and the value is 11111111, which is equal to-11111111.

The binary value of 3 is 00000011, And the inverse value is 11111100, and the value is 11111101, which is equal to-11111101.

The binary value of 4 is 00000100, And the inverse value is 11111011, and the value is, which is equal -.

The bold conclusion is that the negative sign is used for the binary bit of the memory.Returns the inverse value of 1..


Why are you thinking about this?

This is because of the problem of finding the minimum value of signed Char.

I am writing (char) (unsigned char )~ 0> 1) + 1)

The answer in the book is-(char) (unsigned char )~ 0> 1)

I can't figure out why I wrote the answer.The answer is wrong..

The title of this book is <C programming language (2nd) exercises>

What did the negative number do?

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.