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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:29:48+00:00 2026-06-12T02:29:48+00:00

I am working on a program that mixes 64 bit (for some calculations) and

  • 0

I am working on a program that mixes 64 bit (for some calculations) and 32 bit (for space saving storage) unsigned integers, so it is very important I keep them sorted out during arithmetic to avoid overflows

Here’s an example problem

I want to bit shift 1 to the left by an unsigned long n, but I want the result to be an unsigned long long. This will be used in an comparison operation in an if statement, so there is no assignment going on. I’ll give you some code.

void example(unsigned long shift, unsigned long long compare)
{
    if((1<<shift)>compare)
    {
        do_stuff;
    }
}

I suspect that this would NOT do what I want, so would the following do what I want?

void example(unsigned long shift, unsigned long long compare)
{
    if(((unsigned long long)1<<shift)>compare)
    {
        do_stuff;
    }
}

How do I micromanage the bit width of these things? Which operand determines the bit width that the operation is performed with, or is it the larger of the two?

Also, I would like a run down of how this works for other operations too if possible, such as + * / % etc.

Perhaps a reference to a resource with this information would be good, I cannot seem to find a clear statement of this information anywhere. Or perhaps the rules are simple enough to just post. I am not sure.

  • 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-12T02:29:50+00:00Added an answer on June 12, 2026 at 2:29 am

    Which operand determines the bit width that the operation is performed with, or is it the larger of the two?

    For the bit-shifts, it’s the left operand (the one to be shifted) that determines the type that the operation is performed with. If the integer promotions convert it to int or unsigned int, the operation is performed at that type, otherwise at the type of the left operand.

    For the comparison, the result of the shift may then be converted to the type of the other operand. In your example code, the integer constant 1 has type int, hence the shift would be performed at type int, and the result of that converted to unsigned long long for the comparison. Casting works, since the result has a type that is not changed by the integer promotions, as would using a suffixed literal 1ull.

    For the other listed operations, the arithmetic operations (as for comparisons), the type at which the operation is performed is determined by both operands as follows:

    Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:

    • If both operands have the same type, then no further conversion is needed.
    • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
    • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
    • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
    • Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with a program that uses 16bytes 4v4 one byte matrices : unsigned char
I'm working on a program that needs to edit some objects in an Access
I'm working on a program that will work very nicely with a database structure
I'm working on a program that will sort files based on extension I currently
I'm working on a program that reads in users input as a string, which
I'm working on a program that asks for input, then calculates and posts the
I am working on a program that uses IE to display its help pages.
I'm working on a program that will need to delete a folder (and then
I'm working on a program that creates thumbnails of JPEG images on the fly.
I'm working on a program that accesses StarTeam using the StarTeam SDK for .NET.

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.