// file: lecture_18/example_00.cc // // a simple function that doesn't do much // // system include files // #include "example.h" // function: my_function // // arguments: // long arg1: the first argument // long arg2: the second argument // // return: a bool indicating status // bool my_function(long argv1, long argv2) { // how many bytes is a long integer // fprintf(stdout, "a long integer is %d bytes\n", sizeof(argv1)); fprintf(stdout, "an int integer is %d bytes\n", sizeof(int)); fprintf(stdout, "a short integer is %d bytes\n", sizeof(short int)); fprintf(stdout, "a float is %d bytes\n", sizeof(float)); fprintf(stdout, "a double is %d bytes\n", sizeof(double)); // convert the arguments to unsigned char: // note this is dangerous because the range of a long is [-2^31,2^31-1] // the range of a signed char is [-2^7,2^7-1] // signed char a = (signed char)argv1; signed char b = (signed char)argv2; fprintf(stdout, "a = %d (%ld)\n", a, argv1); // do some bit-level stuff // fprintf(stdout, "a = %d, b = %d\n", a, b); fprintf(stdout, "a & b = %d\n", a & b); fprintf(stdout, "a | b = %d\n", a | b); fprintf(stdout, "a ^ b = %d\n", a ^ b); fprintf(stdout, "~a = %d\n", ~a); fprintf(stdout, "a << 1 = %d\n", a << 1); fprintf(stdout, "a >> 1 = %d\n", a >> 1); // short integers // short int s = argv1; fprintf(stdout, "s << 1 = %d\n", s << 1); fprintf(stdout, "s >> 1 = %d\n", s >> 1); // can I bit-shift floats? // //float f = (float)argv1; float f = 27.2727; fprintf(stdout, "f << 1 = %d\n", (unsigned int)f << 1); fprintf(stdout, "f >> 1 = %d\n", (unsigned int)f >> 1); float* jp = &f; unsigned int* jpp = (unsigned int*)(void*)jp; fprintf(stdout, "jpp << 1 = %d\n", *jpp << 1); fprintf(stdout, "jpp >> 1 = %d\n", *jpp >> 1); // masking // unsigned char bbb = (unsigned int)argv1 & MYMASK; fprintf(stdout, "argv1 = %u, bbb = %d\n", (unsigned int)argv1, bbb); // short integers // /* short int ss = argv1 * argv2 & argv1; fprintf(stdout, "ss = %ld\n", ss); */ // exit gracefully // return true; }