Standard (cross-platform) way for bit manipulation

Posted by Kiril Kirov on Stack Overflow See other posts from Stack Overflow or by Kiril Kirov
Published on 2012-10-04T15:07:51Z Indexed on 2012/10/04 15:38 UTC
Read the original article Hit count: 237

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

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 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

© Stack Overflow or respective owner

Related posts about c++

Related posts about c