Skip to content

Instantly share code, notes, and snippets.

@sashadev-sky
Created February 21, 2019 03:22
Show Gist options
  • Save sashadev-sky/41295eaabca09d8eae630d203182db49 to your computer and use it in GitHub Desktop.
Save sashadev-sky/41295eaabca09d8eae630d203182db49 to your computer and use it in GitHub Desktop.
Conversion of Hex to Two's Compliment
// Conversion to Two's Complement
//Note that this works both ways. If you have -30, and want to represent it in 2's complement,
// take the binary representation of 30:
0000 0000 0000 0000 0000 0000 0001 1110
// Invert the digits.
1111 1111 1111 1111 1111 1111 1110 0001
// And add one.
1111 1111 1111 1111 1111 1111 1110 0010
// Converted back into hex, this is 0xFFFFFFE2.
// And indeed, suppose you have this code:
```
#include <stdio.h>
int main() {
int myInt;
myInt = 0xFFFFFFE2;
printf("%d\n",myInt);
return 0;
}
```
// That should yield an output of -30. Try it out if you like.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment