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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:36:49+00:00 2026-05-22T18:36:49+00:00

volatile uint16_t r; unsigned char poly = 0x07; unsigned char c = 0; r

  • 0
volatile uint16_t r;
unsigned char poly = 0x07;
unsigned char c = 0;

r = (c << 8) ^ poly;

When the code is compiled with gcc on Linux, r is 7.
When the same code is compiled by Microchip C18, r is 0.
Why?

If I change it to:

volatile uint16_t r;
uint16_t poly = 0x07;
uint16_t c = 0;

r = (c << 8) ^ poly;

r becomes 7 in C18, too.

There is a section about integer promotion in the C18 manual, but I don’t think it has something to do with my question. Anyway, here it is:

ISO mandates that all arithmetic be performed at int precision or greater.
By default, MPLAB C18 will perform
arithmetic at the size of the largest
operand, even if both operands are
smaller than an int. The ISO mandated
behavior can be instated via the -Oi
command-line option.

  • 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-22T18:36:49+00:00Added an answer on May 22, 2026 at 6:36 pm

    Since c << 8 is undefined in this compiler, the result of xor can’t be predicted. The result could be anything the compiler chooses.

    See What Every C Programmer Should Know About Undefined Behavior for an introduction to undefined behavior, especially the section “Oversized Shift Amounts”.

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

Sidebar

Related Questions

The following code using System.Threading; class Test { volatile int counter = 0; public
Consider the following: volatile uint32_t i; How do I know if gcc did or
try it out: volatile float bob = -344.0f; unsigned int fred = (unsigned int)bob;
The question came up when I saw this code: private static volatile ConcurrentHashMap<String, String>
I have the following declaration in my code: u32 volatile __attribute__((nocast)) *A, *B; Is
What does the volatile keyword do? In C++ what problem does it solve? In
Is there any difference between a volatile Object reference and AtomicReference in case I
In C and C++ a variable can be marked as volatile , which means
I have used a static global variable and a static volatile variable in file
At work today, I came across the volatile keyword in Java. Not being very

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.