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

The Archive Base Latest Questions

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

I’m programming C on an embedded system. The processor architecture is 32 bits (

  • 0

I’m programming C on an embedded system. The processor architecture is 32 bits (sizeof(int) is 32 bits, sizeof(short) is 16 bits). There is a 32-bit variable that is a memory-mapped control register (CTRL_REG) that is specified as only the bottom 16 bits being used, and they contain a signed 16 bit integer value (writing to the higher bits has no effect). Memory accesses must be 32-bit aligned, so I can’t just bump the pointer over a couple bytes, and also I cannot assume an endianness. I’m concerned that automatic type promotion is going to mess with whatever I’m storing by extending the sign bit out to bit 31 instead of leaving it at bit 15 where I want it. What is the best way to store something in this location?

Here was my original code, which I am nearly certain is wrong:

#define CTRL_REG   *((volatile unsigned int *)0x4000D008u)
short calibrationValue;

CTRL_REG = -2 * calibrationValue;

Then I tried this, but I think it still might be subject to integer promotion at the point of the assignment:

CTRL_REG = (short)(-2 * calibrationValue);

Then finally I thought of this:

CTRL_REG = (unsigned short)(short)(-2 * calibrationValue);

I can’t evaluate these options very well because they all work in my tests because calibrationValue happens to be negative (it’s a calibration parameter specific to each device, and so could be positive on some devices), so after multiplying by -2, I end up storing a positive value and thus I don’t actually run into the problem I’m expecting in my tests.

Your help is greatly appreciated, even if it’s just to say “you’re thinking too much”.

  • 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-25T15:02:20+00:00Added an answer on May 25, 2026 at 3:02 pm

    Think about what a -16 ( for example ) looks like in 16 bit : ‘0xFFF0’ , and in 33 bit: ‘0xFFFFFFF0’. Sign extending is exactly what you want to ensure you have a sign bit in the right place. Since the upper 16 are don’t-care, it’s fine to fill them with 1s. So create a signed 32 bit value, then cast it to an unsigned 32 to put in your register:

      Int32 calreg= -2L * calibrationValue;
       CTRl_REG = (Uint32)calreg;
    

    If you would rather write 0s to the high bits, mask with 0xFFFF before the cast.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.