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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:50:22+00:00 2026-06-15T18:50:22+00:00

Suppose I have these two types: typedef unsigned long long uint64; typedef signed long

  • 0

Suppose I have these two types:

typedef unsigned long long uint64;
typedef signed long long sint64;

And I have these variables:

uint64 a = ...;
uint64 b = ...;
sint64 c;

I want to subtract b from a and assign the result to c, clearly if the absolute value of the difference is greater than 2^63 than it will wrap (or be undefined) which is ok. But for cases where the absolute difference is less than 2^63 I want the result to be correct.

Of the following three ways:

c = a - b; // sign conversion warning ignored

c = sint64(a - b);

c = sint64(a) - sint64(b);

Which of the them are guaranteed to work by the standard? (and why/how?)

  • 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-15T18:50:23+00:00Added an answer on June 15, 2026 at 6:50 pm

    None of the three work. The first fails if the difference is negative (no matter the absolute value), the second is the same as the first, and the third fails if either operand is too large.

    It’s impossible to implement without a branch.

    c = b < a? a - b : - static_cast< sint64 >( b - a );
    

    Fundamentally, unsigned types use modulo arithmetic without any kind of sign bit. They don’t know they wrapped around, and the language spec doesn’t identify wraparound with negative numbers. Also, assigning a value outside the range of a signed integral variable results in an implementation-defined, potentially nonsense result (integral overflow).

    Consider a machine with no hardware to convert between native negative integers and two’s complement. It can perform two’s complement subtraction using bitwise negation and native two’s complement addition, though. (Bizarre, maybe, but that is what C and C++ currently require.) The language leaves it up to the programmer, then, to convert the negative values. The only way to do that is to negate a positive value, which requires that the computed difference be positive. So…

    The best solution is to avoid any attempt to represent a negative number as a large positive number in the first place.

    EDIT: I forgot the cast before, which would have produced a large unsigned value, equivalently to the other solutions!

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

Sidebar

Related Questions

Suppose I have an IEnumerable<int> and I want these to be converted into their
Suppose there are two types of messages, QUOTE and TRADE. Both have different fields.
Suppose that I have two data types Foo and Bar. Foo has fields x
HI all ,what i want is to map the images.Suppose i have two images
Suppose we have declared these two classes: public class Animal { //..... } public
Suppose I have these two objects: public class Object1 { string prop1; string prop2;
Suppose i have two classes that inherit from IntentService. Now quoting from the documentation
Suppose you have two linked servers called Local and Remote respectively. Is there a
Suppose i have one folder and inside that another two files are there .
Suppose I have these domain objects: DataPoint ========= public int Id {get;set;} public int

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.