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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:19:23+00:00 2026-05-13T01:19:23+00:00

This question is NOT a duplicate of this question . I came across a

  • 0

This question is NOT a duplicate of this question.

I came across a situation where I might have had to left-shift a (positive) number by a negative value, i.e., 8 << -1. In that case, I would expect the result to be 4, but I’d never done this before. So I made up a little test program to verify my hypothesis:

for (int i = -8; i <= 4; i++)
    Console.WriteLine("i = {0}, 8 << {0} = {1}", i, 8 << i);

which to my shock and surprise gave me the following output:

i = -8, 8 << -8 = 134217728
i = -7, 8 << -7 = 268435456
i = -6, 8 << -6 = 536870912
i = -5, 8 << -5 = 1073741824
i = -4, 8 << -4 = -2147483648
i = -3, 8 << -3 = 0
i = -2, 8 << -2 = 0
i = -1, 8 << -1 = 0
i = 0, 8 << 0 = 8
i = 1, 8 << 1 = 16
i = 2, 8 << 2 = 32
i = 3, 8 << 3 = 64
i = 4, 8 << 4 = 128

Can anyone explain this behaviour?

Here’s a little bonus. I changed the left-shift to a right-shift, and got this output:

i = -8, 8 >> -8 = 0
i = -7, 8 >> -7 = 0
i = -6, 8 >> -6 = 0
i = -5, 8 >> -5 = 0
i = -4, 8 >> -4 = 0
i = -3, 8 >> -3 = 0
i = -2, 8 >> -2 = 0
i = -1, 8 >> -1 = 0
i = 0, 8 >> 0 = 8
i = 1, 8 >> 1 = 4
i = 2, 8 >> 2 = 2
i = 3, 8 >> 3 = 1
i = 4, 8 >> 4 = 0
  • 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-13T01:19:23+00:00Added an answer on May 13, 2026 at 1:19 am

    You can’t shift by a negative value. You also can’t shift by a large positive number.

    From the C# spec (http://msdn.microsoft.com/en-us/library/a1sway8w.aspx):

    If first operand is an int or uint (32-bit quantity), 
    the shift count is given by the low-order five bits of second operand.
    
    ...
    
    
    The high-order bits of first operand are discarded and the low-order 
    empty bits are zero-filled. Shift operations never cause overflows.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
This is not a duplicate of this question . I have to serialize the
I hope this question is not a duplicate but I didn't find anything equivalent
Duplicate of this question . update - This is not an exact duplicate. See
This is NOT a duplicate question, and the problem is driving me crazy. I
This question might not seem programming related at first, but let me explain. I'm
This question may not directly relate to programming. I have noticed that the technology
Possible Duplicate: .NET Controls: Why aren’t all calls thread-safe? This question is not about
Ok this is not duplicate of Alternative to Process.Start() because my question is something
Title borrowed from this question , of which this one is not a duplicate.

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.