0

I have a function that returns the bits of a short (inspired from Converting integer to a bit representation):

bool* bitsToShort(short value) {
    bool* bits = new bool[15];
    int count = 0;
    while(value) {
        if (value&1)
            bits[count] = 1;
        else
            bits[count] = 0;
        value>>=1;
        count++;
    }
    return bits;
}

How can I do the reverse? Converte the array of bits in the short?

Community
  • 1
  • 1
  • With some bit/bitwise operators. Not sure if there's a better way/stdfunction – keyser Dec 06 '13 at 22:37
  • 1
    An implementation with 15-bit shorts does not conform to either the C or the C++ language definition. And even if it's changed to 16, that's a bad assumption to make without checking for overflow. Of course, that has nothing to do with the question. `` – Pete Becker Dec 06 '13 at 22:37
  • @PeteBecker An implementation where `short` has 15 value bits plus one sign bit does conform, and the code already doesn't deal with negative values properly (perhaps it doesn't need to) for other reasons, so if you ignore the sign bit, 15 bits remain. –  Dec 06 '13 at 22:45
  • 1
    @hvd - good point. The assumptions, then, are that `value` is non-negative **and** less than 2^16. – Pete Becker Dec 06 '13 at 22:46
  • @PeteBecker Right, I can see that my comment wasn't very clear, but you got what I meant. –  Dec 06 '13 at 22:51
  • @hvd - your comment was clear. I added the qualification that the value is less than 2^16 because `short` can have **more than** 16 bits, and if it does, it can have values that are greater than 2^16, and this code won't handle them correctly. – Pete Becker Dec 06 '13 at 22:54
  • @hvd - sorry, mentally replace "2^16" with "2^15" throughout all of my comments. I should know better than to post when I'm hungry. – Pete Becker Dec 06 '13 at 23:06
  • Any reason you aren't using `std::bitset`? – Zac Howland Dec 07 '13 at 07:18

3 Answers3

1
short shortFromBits(bool* bits) {
    short res = 0;
    for (int i = 0; i < 15; ++i) {
        if (bits[i]) {
            res |= 1 << i;
        }
    }
    return res;
}

res |= (1<<i) sets the i-th bit in res to one.

Ivan Smirnov
  • 4,365
  • 19
  • 30
1

Like this:

bool* bits = ... // some bits here
short res = 0;
for (int i = 14 ; i >= 0 ; i--) {
    res <<= 1;             // Shift left unconditionally
    if (bits[i]) res |= 1; // OR in a 1 into LSB when bits[i] is set
}
return res;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • In the code given, bits[0] is the LSB; in your code, bits[0] would be the MSB. Downvoted (sorry about that). – mirabilos Dec 06 '13 at 22:45
-1

Essentially:

unsigned short value = 0;
for (int i = sizeof(unsigned short) * CHAR_BIT - 1; 0 <= i; --i) {
    value *= 2;
    if (bits[i)
        ++value;
}

This assumes that bits points to an array of bool with at least sizeof(unsigned short) elements. I have not tested it. There could be an off-by-one error somewhere. But maybe not.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165