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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:26:14+00:00 2026-06-11T21:26:14+00:00

The question should be basic but i am surprised that i had some trouble

  • 0

The question should be basic but i am surprised that i had some trouble get it now. First one is when i glanced ‘C++ primer’ book chapter 5.3. The Bitwise Operators, when author use below code as an example to explain shift operation:

unsigned char bits = 1;     // '10011011' is the corresponding bit pattern
bits << 1;                  // left shift

My head spin a little when i looked at this, where is ‘10011011’ coming from? ‘1’ is not ‘0x01’?

Another question comes from http://c-faq.com/strangeprob/ptralign.html, where author try to unpack structure:

struct mystruct {
    char c;
    long int i32;
    int i16;
} s;

using

unsigned char *p = buf;

s.c = *p++;

s.i32 = (long)*p++ << 24;
s.i32 |= (long)*p++ << 16;
s.i32 |= (unsigned)(*p++ << 8);  // this line !
s.i32 |= *p++;

s.i16 = *p++ << 8;
s.i16 |= *p++;

My question is, p is a pointer to unsigned char(which is 8 bits), right? when building higher bytes of s.i32(24~31, 16~23), *p++ is converted to ‘long'(32bits) before doing left shift, so left shift would not lose bit in *p++, but in

s.i32 |= (unsigned)(*p++ << 8);

*p++ is shifted first, then convert to unsigned int, wouldn’t the bits of *p++ all lost during the shift?

Again i realize i maybe missing some of the basics in C here. Hope someone can give a hand here.

Thanks,

  • 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-11T21:26:15+00:00Added an answer on June 11, 2026 at 9:26 pm

    To answer your second question, performing any arithmetic operation on a char promotes it to a (possibly unsigned) int (including shifting and other bitwise operations), so the integer-size value is shifted, not the 8-bit char value.

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

Sidebar

Related Questions

This should be a very basic design question, but for some reason it doesn't
Sorry for the basic, basic question but I'm having some trouble understanding the RoR
I know this should be a basic question but I am hitting a brick
My question is : from basic which are the necessary jars that should required
This may not be the kind of question one should ask on StackOverflow, but
I know this is a basic question but for some strange reason I am
Sorry for the probably basic question, but I'm trying to understand some code and
This is a really basic question but... I have some code like this var
I'm pretty sure this is a basic question but for some reason i can't
I'm pretty sure this is a basic question but for some reason i can't

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.