#include #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 shift_demo(){ unsigned char temp = (unsigned 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 =41 << 4; printf("41 << 4 = %d\n", temp2); temp2 = 41 >> 4; printf("41 >> 4 = %d\n", temp2); temp2 = -41 << 4; printf("-41 << 4 = %d\n", temp2); temp2 = -41 >> 4; printf("-41 >> 4 = %d\n", temp2); } void fun_demo(){ // Note: skipped this in Spring 2020 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("\nShift demo:\n"); shift_demo(); //printf("\nFun demo:\n"); //int_demo(); return 0; }