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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:41:14+00:00 2026-05-25T01:41:14+00:00

How do I explain the following behavior? #include<iostream> using namespace std; int main(){ unsigned

  • 0

How do I explain the following behavior?

#include<iostream>

using namespace std;

int main(){

       unsigned char a = 8;

       cerr << "a: " << (int)a << '\n';

       unsigned char b = (~a) >> 6;

       cerr << "b: " << (int)b << '\n';

       unsigned char c = (~a);
       c = c >> 6;

       cerr << "c: " << (int)c << '\n';

       return 0;
}

Output:

a: 8
b: 255
c: 3

After further testing it seems that (~a) becomes an int rather than unsigned char. This is why the 1‘s get shifted in.

What’s going on?

EDIT: My compiler is just standard gcc 4.1.2

  • 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-25T01:41:14+00:00Added an answer on May 25, 2026 at 1:41 am

    All arithmetic and bitwise operators in C always widen their arguments to at least int if they were originally shorter integral types. That’s just how the language is defined. The language specification calls this the “integral promotion”.

    (The underlying reason for this is to make it easier to implement C on architectures where the hardware does not support efficient operations on shorter quantities than a full machine word. Of course, it’s also partly just because it has always worked that way and cannot be changed without breaking a lot of existing code that depends on this behavior).

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

Sidebar

Related Questions

The following code: #include <stdint.h> int main() { uint8_t Byte; if (Byte < 0)
Can someone explain the behavior of the following def iterate return yield return end
Please see the following code and explain the output behavior. public class MyFinalTest {
Can anyone explain the following behaviour to a relative newbie... const char cInputFilenameAndPath[] =
Can someone help explain the following: If I type: a=`ls -l` Then the output
Could anyone please explain the following line of code, found on http://docs.openttd.org/ai__cargo_8cpp_source.html return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
Can someone explain the following behaviour (I'm using Visual Studio 2010). header: #pragma once
Could you please explain the following behavior of C# Class. I expect the classResult
Can someone explain the following strange behavior? I run the following program on a
Can anyone explain the following behavior? var ctx = $('#myCanvas').getContext(2d); //doesnt work var ctx

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.