简体   繁体   中英

Best way to grab 6 most significant bits from an unsigned int using operators in C?

I have managed to get rid of the first couple of bits but how would I only keep only the 6 most significant bits in C? I have tried ((num << 6) & 1) with no luck

To get the most significant six bits of an integer and not the rest, bitwise AND it with a mask with the high six bits set and the rest clear.

Because unsigned int is a pure binary type, its maximum value, UINT_MAX , has all its bits set. Then UINT_MAX >> 6 shifts those to the right, producing a result with the high six bits clear and the rest set. Performing a bitwise NOT, ~ (UINT_MAX >> 6) , produces a result with the high six bits set and the rest clear.

Then num & ~ (UINT_MAX >> 6) produces the high six bits of num with the remaining bits clear.

UINT_MAX is declared in <limits.h> . Due to C's wrapping arithmetic for unsigned types, you can also get the maximum value of an unsigned int by using -1u , so num & ~ (-1u >> 6) will also work.

Universal method not depending on the width of the integer

#define  C6B(num) ((num) & ~((1ull << (sizeof(num) * CHAR_BIT - 6)) - 1))

OP wanted the 6 MSbits left in their original place . Other answers address that.


If we want these shifted into the least significant place, a common solution would look like the following. This assumes that an unsigned has 32 bits. Although common, this is not specified by C.

num >> (32 - 6)

Alternatively we could use the below. This assumes there are no padding bits, which is very common for unsigned .

#include <limits.h>
num >> (sizeof num * CHAR_BIT - 6)

Alternatively we could make no assumptions and determine bit width from UINT_MAX and value bits of an integer type :

// Bits in a MAX integer type
// https://stackoverflow.com/a/4589384/2410359
#define IMAX_BITS(m) ((m)/((m)%255+1) / 255%255*8 + 7-86/((m)%255+12))

#include <limits.h>
#define UINT_VALUE_BITS IMAX_BITS(UINT_MAX)

num >> (UINT_VALUE_BITS - 6)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM