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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:32:43+00:00 2026-06-12T10:32:43+00:00

As are are different binary representation of the numbers (for example, take big/little endian),

  • 0

As are are different binary representation of the numbers (for example, take big/little endian), is this cross-platform:

// NOTE: FIXED-SIZE unsigned integral type
some_unsigned_type variable = some_number;

// set n-th bit, starting from 1,
// right-to-left (least significant-to most significant)
variable |= ( 1 << ( n - 1 ) );

// clear the same bit:    
variable &= ~( 1 << ( n - 1 ) );

In other words, does the compiler always take care of the different binary representation of the fixed size unsigned numbers, or it’s platform-specific?

And what if variable is signed integral type (for example, int) and its value is

  • zero
  • positive
  • negative?

What does the Standard say about this?

P.S. And, yes, I’m interesting in both – C and C++, please don’t tell me they are different languages, because I know this 🙂

I can paste real example, if needed, but the post will become too long

  • 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-12T10:32:45+00:00Added an answer on June 12, 2026 at 10:32 am

    Disclaimer: I am implicitly assuming that you are talking about an integer type with a fixed width. Bit-shifting otherwise is quite hazardous…

    Standard: n3337 C++11

    The definition of shifts is mathematical for unsigned types or positive values in signed types (*), and therefore not affected by the underlying hardware representation.

    5.8 Shift operators [expr.shift]

    2 The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-filled. 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. Otherwise, if E1 has a signed type and non-negative value, and E1×2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

    3 The value 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 non-negative 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.

    For the same reason, I would think the bitwise and, or and negate are okay: they are defined mathematically.

    5.3.1 Unary operators [expr.unary.op]

    10 The operand of ˜ shall have integral or unscoped enumeration type; the result is the one’s complement of its operand.

    5.11 Bitwise AND operator [expr.bit.and]

    1 The usual arithmetic conversions are performed; the result is the bitwise AND function of the operands. The operator applies only to integral or unscoped enumeration operands.

    5.13 Bitwise inclusive OR operator [expr.or]

    1 The usual arithmetic conversions are performed; the result is the bitwise inclusive OR function of its operands. The operator applies only to integral or unscoped enumeration operands.

    However I will admit I am less sure for the latter two, I could not find any definition of bitwise XX function, so even though I believe they refer to they mathematical counterparts I can offer no assurance.

    (*) Thanks to phresnel for pointing that out.

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

Sidebar

Related Questions

I'm writing a cross-platform package which need to include different binary file for different
I'm writing different implementations of immutable binary trees in C#, and I wanted my
Was just reading about different algos disassemblers use to identify binary as assembly instructions.
The rules for promotion is when operands are of different types, automatic binary numeric
Many different representation of parameters in python such as : urllib2.urlopen(url[, data][, timeout]) urllib2.build_opener([handler,
I'm trying to make my mind on whether to store a binary representation of
I have two binary files with different sizes. I need to compare first N
I want to replace a binary file if the contents are different. So I
For each of our binary assets we generate a MD5 hash. This is used
Are There different methods to store binary files in SVN? if so, what are

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.