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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:53:42+00:00 2026-05-17T22:53:42+00:00

I’m using atomic operations on counters, and I care about performance. I know that

  • 0

I’m using atomic operations on counters, and I care about performance. I know that on the 64-bit platform incrementing a long can be done in one shot, instead it requires two instructions in 32-bit platforms

I have such a code fragment

#if X64
using COUNTER_TYPE = System.Int64;
#else
using COUNTER_TYPE = System.Int32;
#endif

But I define no X64 constant in my project’s configuration. I wonder if there is a way to determine via #if statements if we are compiling for the x64 or not. When I implement a circular queue, I want to force its size up to 4 billions elements when running on x86, but I may appreciate unlocking its size to long.MaxValue when on x64. I know that on modern processors one or two instructions don’t really care, but it’s still my curiosity to know if I can detect the configuration via code without redefining an x64 profile with such constant specified.

Thank you.

  • 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-17T22:53:43+00:00Added an answer on May 17, 2026 at 10:53 pm

    One way to do this is to use System.IntPtr, which already abstracts the “bitness” of the assembly.

    Note that by default, the C# compiler compiles for “anycpu” which means that the bitness of the assembly at runtime is determined by the bitness of the process into which it is loaded. For executable assemblies, this is x86 for 32 bit OS and x64 for 64 bit OS.

    There are 3 equivalent ways to get the size of IntPtr:

    // Use the static property
    var size = IntPtr.Size;
    
    // Use Marshal
    var size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr));
    
    // Use unsafe with sizeof
    unsafe
    {
        var size = sizeof(IntPtr);
    }
    

    I can’t see any reason not to use the static property for all cases, since it is easiest.

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

Sidebar

Related Questions

No related questions found

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.