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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:25:24+00:00 2026-05-16T17:25:24+00:00

I recently ran into an issue where I wasn’t getting the numerical result I

  • 0

I recently ran into an issue where I wasn’t getting the numerical result I expected. I tracked it down to the problem that is illustrated by the following example:

#include <stdio.h>

int main()
{
  double sample = .5;
  int a = (int)(sample * (1 << 31));
  int b = (int)(sample * (1 << 23) * (1 << 8));
  printf("a = %#08x, b = %#08x\n", a, b);
}
// Output is: a = 0xc0000000, b = 0x40000000

Why is the result of multiplying by (1 << 31) different than the result of multiplying by (1 << 23) * (1 << 8)? I expected the two to give the same answer but they don’t.

I should note that all my floating point values are in the range [-1, 1).

  • 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-16T17:25:25+00:00Added an answer on May 16, 2026 at 5:25 pm

    You are apparently expecting identical results since you assume that to multiply by (1 << 31) is the same as to multiply by (1 << 23) and then by (1 << 8). In general case they are not the same. You are performing the (1 << 31) calculation in a signed int domain. If your platform uses 32-bit ints, the (1 << 31) expression overflows, while both (1 << 23) and (1 << 8) are not overflowing. This immediately means that the result of the first multiplication is unpredictable.

    In other words, it doesn’t make any sense to do (1 << 31) on a platform that has only 31 bits in the value representation of int type. You need at least 32 value-forming bits to meaningfully calculate (1 << 31).

    If you want your (1 << 31) to make sense, calculate in it the unsigned domain: (1u << 31), (1u << 23) and (1u << 8). That should give you consistent results. Alternatively, you can use a larger signed integer type.

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

Sidebar

Related Questions

I recently ran into a problem that I thought boost::lambda or boost::phoenix could help
I recently ran into a problem with a COM object that was using a
I recently ran into a issue where intermediate link betweeen a TCP server and
I'm writing a C parser using PLY, and recently ran into a problem. This
Here's a problem I ran into recently. I have attributes strings of the form
I ran into an interesting behavior recently. It seems that if I override .equals()
A co-worker recently ran into a situation where a query to look up security
Recently I ran into this error in my web application: java.lang.OutOfMemoryError: PermGen space It's
I recently ran out of disk space on a drive on a FreeBSD server.
I recently ran up against a wall doing some bash shell programming where an

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.