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 8024731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:56:50+00:00 2026-06-04T22:56:50+00:00

i am reading a .cpp file containing a unsigned char variable, it’s trying the

  • 0

i am reading a .cpp file containing a unsigned char variable, it’s trying the bitwise left shift 16 bits, since an unsigned char is composed of 8 bits, left shift 16 bits will erase all the bits and fill it with eight 0s.

unsigned char byte=0xff; byte << 16;

  • 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-06-04T22:56:51+00:00Added an answer on June 4, 2026 at 10:56 pm

    When you shift a value,

    unsigned char x = ...;
    int y = x << 16;
    

    The type of x is promoted to int if unsigned char fits in an int (most systems), or to unsigned if unsigned char does not fit in an int (rare1). As long as your int is 25 bits wide or wider, then no data will be discarded2.

    Note that this is completely unrelated to the fact that 16 has type int.

    /* All three are exactly equivalent */
    x << 16;
    x << 16u;
    x << (unsigned char) 16;
    

    Source: from n1516 (C99 draft):

    §6.5.7 paragraph 3: Bitwise Shift Operators

    The integer promotions are performed on each of the operands. The type of the result is
    that of the promoted left operand.

    §6.3.1.1 paragraph 2: Boolean, characters, and integers

    If an int can represent all values of the original type (as restricted by the width, for a
    bit-field), the value is converted to an int; otherwise, it is converted to an unsigned
    int. These are called the integer promotions.

    Footnotes:

    1: Some DSP chips as well as certain Cray supercomputers are known to have sizeof(char) == sizeof(int). This simplifies design of the processor’s load-store unit at the cost of additional memory consumption.

    2: If your left shift is promoted to int and then overflows the int, this is undefined behavior (demons may fly out your nose). By comparison, overflowing an unsigned is always well-defined, so bit shifts should usually be done on unsigned types.

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

Sidebar

Related Questions

I have a int vertexCount variable that's initialized by reading a text file on
After reading this article Storing C++ template function definitions in a .CPP file ,
I am reading source code of hoard memory allocator, and in the file of
I have been reading the ISO 14882:2003. It says: s-char: any member of the
I have been reading a pdf file on OpenGL lighting . It says for
I am trying to learn C++ myself by reading a book and do exercise
I'm trying to work on a cross-platform project that uses boost for the file/directory
I wanna stop the reading of my text input file when the word synonyms
This is my problem. I am reading a file in binary mode, appending the
I've got a function that should read from file line by line, the reading

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.