Created
July 28, 2018 03:33
-
-
Save xcaptain/07ff718c69bf1bf1aae31da3ff4a9fb8 to your computer and use it in GitHub Desktop.
represent a variable in machine code
This file contains 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
#include <stdio.h> | |
typedef unsigned char* byte_pointer; | |
void show_bytes(byte_pointer start, size_t len) { | |
size_t i; | |
for (i = 0; i < len; i++) { | |
printf("%.2x", start[i]); | |
} | |
printf("\n"); | |
} | |
void show_int(int x) { | |
show_bytes((byte_pointer) &x, sizeof(x)); | |
} | |
void show_short(short x) { | |
show_bytes((byte_pointer) &x, sizeof(x)); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
short v = -12345; | |
show_short(v); | |
unsigned short uv = 53191; | |
show_short(uv); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-12345和53191的二进制表示是一样的,说明C语言是使用2补码表示数字的