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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:51:08+00:00 2026-06-01T01:51:08+00:00

I have a method in my C# application which is attempting to pack an

  • 0

I have a method in my C# application which is attempting to pack an integer, but the shift operator is behaving strangely.
The comments in the code below show the contents of code as seen by the debugger after the execution of the line.

        long code = 0;          //0x0000000000000000
        code += (0x1111 << 32); //0x0000000000001111
        code += (0x2222 << 16); //0x0000000022221111
        code += (0x3333);       //0x0000000022224444

Take the first line, I would expect that the code contained in parentheses would be executed first, which would result in 0x1111 being shifted left by 32 bits, yet it isn’t, but on the next line the shift does take place, and in the correct place as well (i.e. 0x2222 must be shifted before the add otherwise the result would be 0x33330000).

What have I missed about the operator that explains this?

  • 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-01T01:51:09+00:00Added an answer on June 1, 2026 at 1:51 am

    What you missed is that bit shift operations on ints are mod 32. Since 0x1111 is an int, the operation is 0x1111 << (32 % 32), which is 0x1111 << 0, which is just 0x1111.

    What you probably want is 0x1111L << 32.

    From section 7.9 of the C# spec:

    • When the type of x is int or uint, the shift count is given by the low-order five bits of count. In other words, the shift count is computed from count & 0x1F.

    • When the type of x is long or ulong, the shift count is given by the low-order six bits of count. In other words, the shift count is computed from count & 0x3F.

    On some hardware architectures, shifting an int by 32 will yield 0, while other architectures will yield the same int. In order to have consistent behavior, the designers of C# had to choose one of those options. The current behavior simply requires performing & 0x1F on the shift count on architectures that otherwise yield 0. However, always returning 0 would require a comparison and a branch on architectures that yield the int. Since branches tend to be expensive, it makes sense that they chose the option that is fastest in the most cases.

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

Sidebar

Related Questions

I have a console application which is parsing HTML documents via the WebRequest method
I have my main application delegate which contains a method that returns an object.
In my application I have methods which returns a control (checkbox, radiobutton, textbox) but
I'm trying to test a method I have in my application, but I don't
I have a asp.net application which is written in the traditional method [SqlConnection. SqlAdapter..
I have a method in my application which retrieves the last saved image in
A few questions have come up recently involving the Application.Evaluate method callable from Excel
I am looking for a method of storing Application Messages, such as You have
I have an application (ASP.NET MVC) that uses a Next New method to get
I have created a console application that calls a method on a webservice. I

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.