Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6381293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:22:38+00:00 2026-05-25T02:22:38+00:00

I ran across the following example program and I don’t exactly understand it’s output:

  • 0

I ran across the following example program and I don’t exactly understand it’s output:

#include <stdio.h>

int main( void ) {

    unsigned char i, m =0xFF, n=0x1;

    for ( i = 0; i != 8; i++,n+=n, m/=2 )
        printf("%5x %5x %5x %5x %5x %5x\n", n,m,n&m,n|m,n^m,~n);

    return 0;
}

It prints out:

    1    ff     1    ff    fe fffffffe
    2    7f     2    7f    7d fffffffd
    4    3f     4    3f    3b fffffffb
    8    1f     8    1f    17 fffffff7
   10     f     0    1f    1f ffffffef
   20     7     0    27    27 ffffffdf
   40     3     0    43    43 ffffffbf
   80     1     0    81    81 ffffff7f

The problem is that last column. Since it’s unsigned char I would expect it to print out just 2 places in every column. ~n produces an unsigned char as it’s result, but it seems like it’s being cast to a signed 32 bit value and sign extended by the %5x specifier.

How is that possible, what’s going on here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:22:39+00:00Added an answer on May 25, 2026 at 2:22 am

    Integer types are promoted when they are used in arithmetic operations (this has nothing to do with printf, by the way).

    So, for example,

    unsigned char x = 0xff;
    int y = ~x; // x is promoted to 0x000000ff, then changed to 0xffffff00
    unsigned char z = ~x; // truncated back to 0x00
    

    Integer promotion causes various problems:

    unsigned char x = 1;
    if (x << 8)
        puts("x << 8 is true"); // does print
    x <<= 8;
    if (x)
        puts("x <<= 8 is true"); // does not print
    

    The two ways to truncate things are casting and masks. Use whatever you prefer.

    unsigned char x = 0xab;
    printf("x = %02x\n", (unsigned char) x);
    printf("x = %02x\n", x & 0xff);
    

    Integer promotion doesn’t always happen, and it’s not the only kind of implicit cast. It’s also a bit subtle and the exact rules are difficult to remember. You only really need to worry about it if you’re working with 64-bit numbers, because 1U << 32 could end up being 0 or 1 or something else entirely. (It’s often 1 on x86).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While looking at the Timer documentation I ran across the following example with this
I just ran across the following line of code: if(someInt <= int.MinValue) { /*
I ran across the following code in Ely Greenfield's SuperImage from his Book component
I ran across the following in a stored procedure: for select 1 from scan_queue
I ran across the following code in one of our in-house dlls and I
I just ran across the following error: (.gnu.linkonce.[stuff]): undefined reference to [method] [object file]:(.gnu.linkonce.[stuff]):
I recently ran across some 3rd party C# code which does the following: public
I ran the following comand git whatchanged 7c8358e.. --oneline and got the below output.
I ran across the following class in a C# XNA graphics api and I
I just ran across the following syntax in a piece of Haskell code -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.