How to find the most significant bit of a signed integer in C

Future-Escape5902

I need to find the most significant bit of signed int N and save it in signBitN. I want to do this using bitwise only operations. Also, how would I make signBitN extend so that all its bits are equal to its significant bit. i.e. if it's significant bit was zero, how would I extend that to be 00000...00? The closest I've gotten is signBitN=1&(N>>(sizeof(int)-1));

tstanisl

Portable expression:

1 & (x >> (CHAR_BIT * sizeof(int) - 1))

Latest C standards put 3 standards on representation of ints.

  • sign and magnitude
  • one complement
  • two complement See section 6.2.6.2 Integer types of C11 standard.

Only the third option is relevant in practice for modern machines. As specified in 6.2.6.1:

Values stored in non-bit-field objects of any other object type consist of n x CHAR_BIT bits, where n is the size of an object of that type, in bytes.

Therefore int will consist of sizeof(int) * CHAR_BIT bits, likely 32. Thus the highest bit of int can be read by shifting right by sizeof(int) * CHAR_BIT - 1 bits and reading the last bit with bitwise & operator.

Note that the exact value of the int after the shift is implementation defined as stated in 6.5.7.5.

On sane machines it would be:

int y = x < 0 ? -1 : 0;

The portable way would be casting between int and an array of unsigned char and setting all bytes to -1. See 6.3.1.3.2:

if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

And 6.2.6.1.2

Values stored in unsigned bit-fields and objects of type unsigned char shall be represented using a pure binary notation.

You can use memset() for that.

int x;
memset(&x, (x < 0 ? -1 : 0), sizeof x);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to test the most significant bit of signed or unsigned integer?

Find most significant bit in Swift

Negation of least significant bit (LSB) of signed integer

set most significant bit in C

Find most significant set bit in a long

What is the fastest/most efficient way to find the least significant set bit in an integer in R?

How to clear most significant bit in byte?

Set the most significant bit

C++: How to get the MSB (Most-Significant Bit) of a Bitset (using bitwise operators)?

How can you get the j first most significant bits of an integer in C++?

how is the most significant bit radix sort more efficient than the least significant bit radix sort?

Fast way of finding most and least significant bit set in a 64-bit integer

How do I flip the most significant bit in MIPS?

bitwise most significant set bit

C: How to save the result of 2 32 bit unsigned integer into a signed integer

How to work with integer limits in C++. The number "-91283472332" is out of the range of a 32-bit signed integer

bit shifting signed integer

How do I convert a float to a 16 bit signed integer fraction in C?

Comparing the Most Significant Bit of two numbers: ==, <, <=

Efficiently getting most and least significant bit in javascript

How do I convert a signed 8-bit byte to a signed 16-bit integer in assembly?

How to format float in C without most significant digits?

fastest way to access the least significant bit of an integer?

How to convert signed 16 bit integer to unsigned 16 bit integer in Java?

How to put 32-bit signed integer into higher 32 bits of 64-bit unsigned integer?

How to interpret using Most Significant Bit of single byte - Pcapng time format "if_tsresol"

How to change the most significant bit to a 1 after shifting an int to the right using '>>'?

Extract bits of a register starting with the most significant bit, or a high bit

Get the most significant bit from an 8-bit value