C language Printing 16 binary 0xffffff phenomenon problem analysis!

Source: Internet
Author: User

Today in Bo asked to see a friend's problem, roughly in the network program, printed out of the 16 number, inexplicable appearance ffffff. For example, a byte is really worth 0xc9, and printing out is 0xffffffc9. The original Bo asked the connection as follows: http://q.cnblogs.com/q/71073/

In fact, a similar problem is not only in the network program will appear, see the Sample code:

  1#include <stdio.h>2 intMain ()3 {  4     Charc =0xc9; 5printf"a:c =%2x\n", (unsignedChar) c); 6printf"b:c =%2x\n", C &0xFF); 7printf"c:c =%2x\n", c); 8     return 0; 9}

The program output is as follows:

A:C == = Ffffffc9

can see:

Converting c to unsigned char printing is correct. As a condition.

C and 0xFF do & operation after printing correctly. As a condition B.

c do not do any processing, the problem is reproduced, print out FFFFFFC9. As a situation c.

Situation a B is my Baidu to solve the C phenomenon of the method. So let's now analyze and explain the ABC three cases one by one.


  First we have to know that the printf () function's%x (x) outputs the 16 binary format of the int type. So c variables of char type are converted to int type.

Secondly, we know that the computer uses the complement to represent the data. For the original code, anti-code, complementary knowledge, please charge yourself.

Case C:

C's Complement: 11001001 (0XC9).

   C's anti-code: 11001000 (0XC9).

   The original code of C: 10110111 (0XC9). 

Because the char type is signed, the highest bit of 1 is considered a minus.

Convert c to int type char-----> int

Int_c The original code: 10000000 00000000 00000000 00110111 (the highest digit of the C source code is referred to the highest bit.) The remaining highs are 0).

   Anti-code for Int_c: 11111111 11111111 11111111 11001000

   Int_c's complement: 11111111 11111111 11111111 11001001 (0XFFFFFFC9).

So it's reasonable to print out a seemingly bizarre value. How to avoid it? See AB case.

Case B:

int_c's complement: 11111111 11111111 11111111 11001001 ( 0XFFFFFFC9).

& /span>

00000000 00000000 00000000 11111111

The end result is:00000000 00000000 00000000 11001001 (0xc9).

case A:


C's Complement: 11001001 (0XC9).

   C's anti-code:11001001(0xc9).

   The original code of C:11001001(0xc9).

Here the cast c is unsigned char type. So the highest bit of 1 is not the plus sign

Convert c to int type char-----> int

The original code of Int_c: 00000000 00000000 00000000 11001001(the highest bit of the C original code is referred to the highest bit 1. The remaining highs are 0).

   Int_c Anti-code:00000000 00000000 00000000 11001001

   Int_c's complement:00000000 00000000 00000000 11001001(0xc9).

So the print is normal.

The above analysis, if there is an incorrect place please correct me.

C language Printing 16 binary 0xffffff phenomenon problem analysis!

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.