Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract n most significant non-zero bits from int in C++ without loops

I want to extract the n most significant bits from an integer in C++ and convert those n bits to an integer.

For example

int a=1200;
// its binary representation within 32 bit word-size is
// 00000000000000000000010010110000

Now I want to extract the 4 most significant digits from that representation, i.e. 1111

00000000000000000000010010110000
                     ^^^^

and convert them again to an integer (1001 in decimal = 9).

How is possible with a simple c++ function without loops?

like image 265
linello Avatar asked Mar 15 '12 11:03

linello


People also ask

How to get the most and least significant bits of an integer?

To get the most-significant bits (MSB) of an integer value, perform a bitwise and between it and the value shown in the following method: public static int GetMSB (int intValue) { return (intValue & 0xFFFF0000); } To get the least-significant bits (LSB) of a value, use the following method:

How to check least significant bit (LSB) of a number?

Also read - Program to check Least Significant Bit (LSB) of a number. We use bitwise AND & operator to check status of any bit. Bitwise AND operation evaluate each bit of resultant value as 1, if corresponding bit of operands is 1. Step by step descriptive logic to check MSB of a number. Input a number from user.

How to find the number of bits required by an integer?

Then multiply it by 8 to get number of bits required by integer. Store total bits in some variable say bits = sizeof (int) * 8;. Read more - How to find size of a data type using sizeof () operator. To get MSB of the number, move first bit of 1 to highest order.

What is the most significant bit of a positive number?

Important note: Most Significant Bit of positive number is always 0 (in 2s complement) and negative number is 1. Enter any number: -1 MSB of -1 is set (1). Bitwise operator programming exercises index. C program to get highest set bit of a number. C program to get lowest set bit of a number.


1 Answers

Some processors have an instruction to count the leading binary zeros of an integer, and some compilers have instrinsics to allow you to use that instruction. For example, using GCC:

uint32_t significant_bits(uint32_t value, unsigned bits) {
    unsigned leading_zeros = __builtin_clz(value);
    unsigned highest_bit = 32 - leading_zeros;
    unsigned lowest_bit = highest_bit - bits;

    return value >> lowest_bit;
}

For simplicity, I left out checks that the requested number of bits are available. For Microsoft's compiler, the intrinsic is called __lzcnt.

If your compiler doesn't provide that intrinsic, and you processor doesn't have a suitable instruction, then one way to count the zeros quickly is with a binary search:

unsigned leading_zeros(int32_t value) {
    unsigned count = 0;
    if ((value & 0xffff0000u) == 0) {
        count += 16;
        value <<= 16;
    }
    if ((value & 0xff000000u) == 0) {
        count += 8;
        value <<= 8;
    }
    if ((value & 0xf0000000u) == 0) {
        count += 4;
        value <<= 4;
    }
    if ((value & 0xc0000000u) == 0) {
        count += 2;
        value <<= 2;
    }
    if ((value & 0x80000000u) == 0) {
        count += 1;
    }
    return count;
}
like image 160
Mike Seymour Avatar answered Oct 31 '22 01:10

Mike Seymour