Skip to content

Instantly share code, notes, and snippets.

@rakaadinugroho
Created March 17, 2020 15:29
Show Gist options
  • Save rakaadinugroho/8c7c16087c064c7d63e68daf2c83459d to your computer and use it in GitHub Desktop.
Save rakaadinugroho/8c7c16087c064c7d63e68daf2c83459d to your computer and use it in GitHub Desktop.
Machine Level Representation Homework
/* 3.55 */
long decode(long x, long y, long z) {
long tmp = y - z;
return (tmp * x) ^ (tmp << 63 >> 63);
}
/* 3.57 */
#include <stdio.h>
#include <assert.h>
long cread(long *xp) {
return (xp ? *xp : 0);
}
long cread_alt(long *xp) {
return (!xp ? 0 : *xp);
}
int main(int argc, char* argv[]) {
long a = 0;
assert(cread(&a) == cread_alt(&a));
assert(cread(NULL) == cread_alt(NULL));
return 0;
}
/* 3.58 */
/* Enumerated type creates set of contants numbered 0 and upward */
typedef enum { MODE_A, MODE_B, MODE_C, MODE_D, MODE_E } mode_t;
long switch3(long *p1, long *p2, mode_t action) {
long result = 0;
switch(action) {
case MODE_A:
result = *p2;
*p2 = *p1;
break;
case MODE_B:
*p1 = *p1 + *p2;
result = *p1;
break;
case MODE_C:
*p1 = 59;
result = *p2;
break;
case MODE_D:
*p1 = *p2;
result = 27;
break;
case MODE_E:
result = 27;
break;
default:
result = 12;
break;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment