I am attempting to display a string representation of an int16_t's Two's Complement. I find the two's complement by (uint16_t)~value + 1;
How would I add the 16 bits to a string?
char* getBits(const int16_t value) {
uint16_t complement = (uint16_t)~value + 1;
char bits = malloc(16*sizeof(char*))
for (int i = 0; i < 16; i++) {
bits[i] = // something
}
return bits;
}
Shift
complementrightibits, and test whether the low-order bit is0or1and put the corresponding character inbits.Also, you need to allocate an extra character in your string for the null terminator. And
bitsneeds to be a pointer, while the element size issizeof(char), notsizeof(char*).There's no need to use the formula
(uint16_t)~value + 1. Converting a signed int to unsigned int automatically returns its twos complement value. So you can simply do: