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

  • SEARCH
  • Home
  • 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 6609575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:44:02+00:00 2026-05-25T19:44:02+00:00

We found some strange values being produced, a small test case is below. This

  • 0

We found some strange values being produced, a small test case is below.
This prints “FFFFFFFFF9A64C2A” . Meaning the unsigned long long seems to have been sign extended.
But why ?
All the types below are unsigned, so what’s doing the sign extension ? The expected output
would be “F9A64C2A”.

#include <stdio.h>

int main(int argc,char *argv[])
{
    unsigned char a[] = {42,76,166,249};

    unsigned long long ts;
    ts = a[0] | a[1] << 8U | a[2] << 16U | a[3] << 24U;

    printf("%llX\n",ts);


    return 0;

}
  • 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-25T19:44:02+00:00Added an answer on May 25, 2026 at 7:44 pm

    In the expression a[3] << 24U, the a[1] has type unsigned char. Now, the “integer promotion” converts it to int because:

    The following may be used in an expression wherever an int or unsigned int may
    be used:

    […]

    If an int can represent all values of the original type, the value is converted to
    an int;
    otherwise, it is converted to an unsigned int.

    ((draft) ISO/IEC 9899:1999, 6.3.1.1 2)

    Please note also that the shift operators (other than most other operators) do not do the “usual arithmetic conversions” converting both operands to a common type. But

    The type of the result is that of the promoted left operand.

    (6.5.7 3)

    On a 32 bit platform, 249 << 24 = 4177526784 interpreted as an int has its sign bit set.

    Just changing to

    ts = a[0] | a[1] << 8 | a[2] << 16 | (unsigned)a[3] << 24;
    

    fixes the issue (The suffix Ufor the constants has no impact).

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

Sidebar

Related Questions

Debugging some finance-related SQL code found a strange issue with numeric(24,8) mathematics precision. Running
I have found some info on the subject ( like this link) , but
I just found some strange behavior of database's order by clause. In string comparison,
after doing some test-code for this link : Is it safe to call temporary
I found some strange behavior when using nested Spring transactions: when, in the same
I have tried using the FileUpload control in ASP.net and found some strange problems.
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
I found some code in a project which looks like that : int main(int
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found some libraries or web services in PHP that does the job.

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.