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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:46:21+00:00 2026-06-03T09:46:21+00:00

Learning the hard way, I tried to left shift a long long and uint64_t

  • 0

Learning the hard way, I tried to left shift a long long and uint64_t to more than 32 bits on an x86 machine resulted 0. I vaguely remember to have read somewhere than on a 32 bit machine shift operators only work on the first 32 bits but cannot recollect the source.
I would like to know is if Shifting more than 32 bits of a uint64_t integer on an x86 machine is an Undefined Behavior?

  • 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-03T09:46:22+00:00Added an answer on June 3, 2026 at 9:46 am

    The standard says (6.5.7 in n1570):

    3 The integer promotions are performed on each of the operands. The type of the result is
    that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

    4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with
    zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced modulo
    one more than the maximum value representable in the result type. If E1 has a signed
    type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is
    the resulting value; otherwise, the behavior is undefined.

    5 The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type
    or if E1 has a signed type and a nonnegative value, the value of the result is the integral
    part of the quotient of E1 / 2E2 . If E1 has a signed type and a negative value, the
    resulting value is implementation-defined.

    Shifting a uint64_t a distance of less than 64 bits is completely defined by the standard.

    Since long long must be at least 64 bits, shifting long long values less than 64 bits is defined by the standard for nonnegative values, if the result doesn’t overflow.

    Note, however, that if you write a literal that fits into 32 bits, e.g. uint64_t s = 1 << 32 as surmised by @drhirsch, you don’t actually shift a 64-bit value but a 32-bit one. That is undefined behaviour.

    The most common results are a shift by shift_distance % 32 or 0, depending on what the hardware does (and assuming the compiler’s compile-time evaluation emulates the hardware semantics, instead of nasal demons.)

    Use 1ULL < 63 to make the shift operand unsigned long long before the shift.

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

Sidebar

Related Questions

I'm currently learning C through Learning C the Hard Way I am a bit
I'm learning Python using Learn Python The Hard Way . It is very good
I'm trying to learn Python with the help of Learning Python the Hard Way.
Ihaven't done much MVC and still learning the hard way on how to do
I'm working my way through Learning Ruby the Hard Way online; I've just finished
Very simple question.. I'm into learning Blend (way hard for developer..) So. I'm editing
I'm learning about Python Packages from Learn Python the Hard Way and one of
i'm reading zed shaw's book learning python the hard way. Forgive me but i'm
I'm learning shell scripting, and am finding it hard finding a good way to
I'm just now learning to programming at age 17. It's hard for me to

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.