Skip to content

Instantly share code, notes, and snippets.

@xcaptain
Created July 28, 2018 03:33
Show Gist options
  • Save xcaptain/07ff718c69bf1bf1aae31da3ff4a9fb8 to your computer and use it in GitHub Desktop.
Save xcaptain/07ff718c69bf1bf1aae31da3ff4a9fb8 to your computer and use it in GitHub Desktop.
represent a variable in machine code
#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;
}
@xcaptain
Copy link
Author

-12345和53191的二进制表示是一样的,说明C语言是使用2补码表示数字的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment