Suppose I have a binary string: 1111 1001 which in decimal is -121 (sign magnitude form). But the computer read this as two number: -7 and -1. From -7 and -1, how can I recover -121.?


First: I assume 1111 and 1001 are read as -1 and -7 (Not -7 and -1!).
Second: I assume for some reason your computer is working with 4 bit values. I am going to use unsigned 8 bit variables for those for now.

The simplest way to combine the two four bit values back to an 8 bit value is to concatenate the bits:

#include <stdint.h>

int8_t u8;
uint8_t u4a,u4b;

  u4a = 0x0F; // '-1' in 4 bits
  u4b = 0x09; // '-7' in 4 bits

  u8  = (u4a<<4)|u4b;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.