Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to sign extend a number in C/C++

Tags:

c++

c

bit

I have a 10 bit signed value. It's actually a 10 bit color value that i want to convert to an int.

The 10 bits are to be interpreted as a signed value within a larger int, where the other bits are zero.

I can think of 3 ways, shown here:

#include <stdio.h>

void p(int v)
{
    printf("\t%d", v);
}

int main()
{
    for (int i = -2; i < 3; ++i)
    {
        unsigned int u = i & 0x3ff;

        int v;

        // method 1: test the bit
        v = u;
        if (u & 0x200) v = v ^ -0x400;
        p(v);

        // method 2: shift
        v = u<<(sizeof(int)*8-10);
        v >>= (sizeof(int)*8-10);
        p(v);

        // method 3: use sign extend
        v = (signed char)(u >> 2);
        v <<= 2;
        v |= u;
        p(v);


        printf("\n");

    }

    return 0;
}

is there a better way? Often there is with this bit twiddling problems.

Thanks for any input.

Update

Adding two more methods. The first suggested by @cruz-jean is bit fields, and the second suggested by gcc after compiling the bitfields.

      // method 4: bit fields
        struct { int v:10; } f;
        f.v = u;
        v = f.v;

        // method 5: sign extend short
        v = (signed short)(u << 6);
        v >>= 6;

Out of interest it appears MSVC compiles #4 into #2 and gcc compiles #4 into #5.

Method 5 looks good.

like image 742
jkj yuio Avatar asked Oct 24 '25 15:10

jkj yuio


2 Answers

You could declare a 10-bit signed bitfield helper type and access the value as an int:

struct signed_10_bit
{
    int val : 10;
};

int some_10_bit_num = 0x3ff;
int extended = signed_10_bit{ some_10_bit_num }.val;
like image 76
Cruz Jean Avatar answered Oct 26 '25 05:10

Cruz Jean


Another one to try:

v = u | (0 - (u&0x200));

good for cpus where shift is slow.

like image 20
Chris Dodd Avatar answered Oct 26 '25 06:10

Chris Dodd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!