Created
February 21, 2019 03:22
-
-
Save sashadev-sky/41295eaabca09d8eae630d203182db49 to your computer and use it in GitHub Desktop.
Conversion of Hex to Two's Compliment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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