Skip to content

Instantly share code, notes, and snippets.

@Gumball12
Last active September 28, 2019 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gumball12/881ccc7e8a852e6c9314b30d0e98e765 to your computer and use it in GitHub Desktop.
Save Gumball12/881ccc7e8a852e6c9314b30d0e98e765 to your computer and use it in GitHub Desktop.
Bit-level operation
// cs-assignment 2-2 "Byte Manipulation - Byte Level Operation"
// @author shj
// import modules
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STR_SIZE 10 // string size
#define TESTING_TIMES 30 // for unit-testing
// declare prototype
int testing();
// define enum type for serve dynamic types in 'generateRandomValue' function
typedef enum {
INT,
DOUBLE,
CHAR,
STRING
} TYPE; // TYPE type
/**
* generate random alphabet
* => ascii 65-90
* @return random alphabet [A-Z]
* */
char generateRandomAlphabet() {
return rand() % 26 + 65; // [A-Z]
}
/*
* generate random value
* @param t specific value's type
* @param x value (void pointer is used to serve dynamic types)
* */
void generateRandomValue(TYPE t, void *x) {
switch (t) { // check type
case INT:
// generate a random integer type value
*(int *) x = rand();
break;
case DOUBLE:
// generate a random double type value
*(double *) x = rand() + rand() / (double) rand();
break;
case CHAR:
// generate a random char type value
*(char *) x = generateRandomAlphabet();
break;
case STRING:
// generate random char type values
for (int i = 0; i < STR_SIZE; i++) {
// insert alphabet into the 'x'
((char *)x)[i] = generateRandomAlphabet();
}
break;
}
}
/**
* use AND(&) operation calculator
* @param a
* @param b
* */
int calc_1(int a, int b) {
return (a & 0x000000ff) + (b & 0xffffff00);
}
/**
* use shift operation calculator
* @param a
* @param b
* */
int calc_2(int a, int b) {
return ((unsigned int) (a << 24) >> 24) + ((b >> 8) << 8); // 4 * moved byte
}
/**
* use memory manipulation
* WARNING: this function will be mutated 'b' value
* @param a
* @param b
* */
int calc_3(int a, int b) {
*((unsigned char *) &b) = *((unsigned char *) &a);
return b;
}
/**
* main function
* */
int main(void) {
// random
srand(time(NULL));
// testing
if (testing() != 0) {
printf("in process...\n");
// do operation 10 times
for (int i = 0; i < 10; i++) {
// generate random int values
int a, b;
generateRandomValue(INT, &a);
generateRandomValue(INT, &b);
// calcuate
printf("\n==> value: 0x%X, 0x%X\n", a, b);
printf("use AND(&) operation: 0x%X\n", calc_1(a, b));
printf("use shift operation: 0x%X\n", calc_2(a, b));
printf("use Memory manipulation: 0x%X\n", calc_3(a, b));
}
}
return 0;
}
/**
* unit-testing function
* @return testing result
* */
int testing() {
// printf msg
printf("=== start testing... ===\n\n");
// init var
int result = 1; // true
// start testing
for (int i = 0; i < TESTING_TIMES; i++) {
// gen random values
int ri;
generateRandomValue(INT, &ri);
double rd;
generateRandomValue(DOUBLE, &rd);
char rc;
generateRandomValue(CHAR, &rc);
char *rs = malloc(STR_SIZE * sizeof(char));
generateRandomValue(STRING, rs);
// print values
printf("\n=== testing %d ===\n", i + 1);
printf("random int: %d\n", ri);
printf("random double: %lf\n", rd);
printf("random char: %c\n", rc);
printf("random string: %s\n", rs);
// free
free(rs);
}
// check test result
if (result != 0) {
printf("\n\n=== success testing ===\n\n");
} else {
printf("\n\n=== failed to testing ===\n\n");
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment