#include typedef unsigned char byte; void show_bytes(void * start, size_t len){ printf("%lu bytes from %p:", len, start); int i; for(i = 0; i < len; i++){ printf(" %.2x", ((byte *) start)[i]); } printf("\n"); } void memory_demo(){ char s[16] = "Cecil Sagehen"; show_bytes(&s,16); double d = 3.1415926535897931; show_bytes(&d, 8); } void shift_demo(){ char temp = (char) (0x41 << 4); // cast truncates to one byte printf("0x41 << 4 = %.2x\n", temp); temp = 0x41 >> 4; printf("0x41 >> 4 = %.2x\n", temp); int temp2 =47 << 4; printf("47 << 4 = %d\n", temp2); temp2 = 47 >> 4; printf("47 >> 4 = %d\n", temp2); temp2 = -47 << 4; printf("-47 << 4 = %d\n", temp2); temp2 = -47 >> 4; printf("-47 >> 4 = %d\n", temp2); } void int_demo(){ int x = 7; int y = x & 1; printf("%d & 1 = %d\n",x,y); x = -14; y = (x + 7) & 0xFFFFFFF8; printf("(%d + 7) & OxFFFFFFF8 = %d\n",x,y); void * p = &x; void * p2 = (void *) ((long)p & ~((long)0x3FF)); void * p3 = (void *) (((long)p >> 10) << 10); printf("%p & ~0x3FF = %p\n",p,p2); printf("((%p >> 10) << 10) = %p\n",p,p3); void * p4 = (void *) ((long)p & ((long)0x3FF)); printf("%p & 0x3FF = %p\n",p,p4); } int main() { printf("Memory demo:\n"); memory_demo(); printf("\nShift demo:\n"); shift_demo(); printf("\nInt demo:\n"); int_demo(); return 0; }