#include <stdio.h>

/*
 *  In this lab you will do some investigation of types.
 *  We'll use C for this because it is a typed language
 *  but allows significantly more flexibility than many
 *  other typed languages.
 * 
 *  To start with, take a look at the function below.
 *  It is designed to print out the bit representation of
 *  any variable.  We will be using this in the code that 
 *  follows.
 */

// prints out all the bits in a particular stored variable
void printBits(void* ptr, int nbytes) {
    // loop over all bytes
    for (int i = 0; i < nbytes; i++) {
        unsigned char b = *(char*)(ptr+i);
        //printf("\n%d\n",b);
        // loop over 8 bits in this byte
        for (int j = 128; j > 0; j /= 2) {
            printf("%d",(b&j)>0);
        } 
        printf(" ");
    }
    printf("\n");
}

/*
 *  Below is a struct definition for later use.
 *  The struct in C is a sort of forerunner to objects in
 *  later languages like C++ and Java.  It packages data
 *  together into a single unit, but there are no methods
 *  directly associated with the data, and no constructors.
 */

typedef struct {
  int x;
  float y;
  char* s;
} MyStruct;

/*
 *  Below is a procedure that demonstrates the use of the 
 *  printBits function.  We create several variables and look
 *  at the bit patterns used to represent them.
 *  
 *  One of the variables is a struct, which we look at as a whole
 *  and also in terms of its contituent parts.  Look carefully
 *  at the corresponding bit patterns and see if you can identify
 *  where each part is located within the whole.
 */

void example_one() {
    printf("Begin example one.\n");
    char a = 'f';
    printf("102 as char: ");
    printBits((void*)&a,sizeof(char));
    int b = 102;
    printf("102 as int: ");
    printBits((void*)&b,sizeof(int));
    float c = 102;
    printf("102 as float: ");
    printBits((void*)&c,sizeof(float));
    double d = 102;
    printf("102 as double: ");
    printBits((void*)&d,sizeof(double));
    
    printf("\n");
    MyStruct s;
    s.x = 102;
    s.y = 102;
    s.s = "Test";
    printf("Entire struct: ");
    printBits((void*)&s,sizeof(MyStruct));
    printf("x field of struct: ");
    printBits((void*)&s.x,sizeof(int));
    printf("y field of struct: ");
    printBits((void*)&s.y,sizeof(float));
    printf("s field of struct: ");
    printBits((void*)&s.s,sizeof(char*));
    printf("value of s field of struct: ");
    printBits((void*)s.s,4*sizeof(char));
    printf("End of example one.\n");
}

typedef struct {
    short s;
    int i;
    long l;
    short* a;
} MyStruct2;

/* 
 *  Exercise 1
 * 
 *  For this exercise, you will write code similar to 
 *  the example above, to reveal the structure of the 
 *  following values:
 *   - a short equal to 1000
 *   - an int equal to 1000
 *   - a long equal to 1000
 *   - an array of short, length 2, with 1000 in the first 
 *     element and zero in the second
 *   - the pointer to that array (its memory address)
 *   - a new struct type, defined by you, containing
 *     each of the values listed above
 */

void exercise_one() {
    printf("\nBegin exercise one.\n");
    short s = 1000;
    printf("1000 as short: ");
    printBits((void*)&s,sizeof(short));
    int i = 1000;
    printf("1000 as int: ");
    printBits((void*)&i,sizeof(int));
    long l = 1000;
    printf("1000 as long: ");
    printBits((void*)&l,sizeof(long));
    short a[2] = {1000,0};
    printf("1000 as array: ");
    printBits((void*)a,2*sizeof(short));
    printf("Pointer to array: ");
    printBits((void*)&a,sizeof(short*));
    MyStruct2 m;
    m.s = 1000;
    m.i = 1000;
    m.l = 1000;
    m.a = a;
    printf("New struct: ");
    printBits((void*)&m,sizeof(MyStruct2));
    printf("End of exercise one.\n");
}

/*
 *  Example 2:  Non-converting cast
 * 
 *  In this example we show how the same bit pattern can be
 *  copied into a new variable of a different type.
 *  When this happens, the bit pattern is interpreted differently
 *  and will therefore have a new value.
 * 
 *  We also show how a multi-bit value can be built up out of
 *  an array of byte-sized characters and then cast to 
 *  some other type.
 */

void example_two() {
    printf("\nBegin example two.\n");
    int a = 1000000000;
    printf("1,000,000,000 as int: ");    
    printBits((void*)&a,sizeof(int));
    float f1 = *((float*)(&a));  // non-converting cast
    printf("non-converting cast to float: ");    
    printBits((void*)&f1,sizeof(float));
    float f2 = (float)a;  // converting cast
    printf("converting cast to float: ");    
    printBits((void*)&f2,sizeof(float));
    printf("int printed as int:  %d\n",a);
    printf("int printed as float:  %f\n",a);
    printf("f1 printed as int:  %d\n",f1);
    printf("f1 printed as float:  %f\n",f1);
    printf("f2 printed as int:  %d\n",f2);
    printf("f2 printed as float:  %f\n",f2);

    // assemble a value from bytes
    char arr[4];
    arr[0] = 0;
    printf("First byte: ");
    printBits((void*)(arr),sizeof(char));
    arr[1] = 202;
    printf("Second byte: ");
    printBits((void*)(arr+1),sizeof(char));
    arr[2] = 154;
    printf("Third byte: ");
    printBits((void*)(arr+2),sizeof(char));
    arr[3] = 59;
    printf("Fourth byte: ");
    printBits((void*)(arr+3),sizeof(char));
    printf("All four bytes together: ");
    printBits((void*)arr,4*sizeof(char));
    int b = *((int*)arr);  // non-converting cast
    printf("non-converting cast to int: ");
    printBits((void*)arr,4*sizeof(char));    
    printf("Value printed as int:  %d\n",b);
    printf("End of example two.\n");    
}

/* 
 *  Exercise 2
 * 
 *  For this exercise, you will write code similar to 
 *  the example above, to do the following:
 *   - Create a double, initialized to 3.14159265
 *   - Copy its value to aa long using a non-converting 
 *     cast
 *   - Copy its value to a second long using a converting 
 *     cast
 *   - Print both variables both as long and as double
 *     and observe the result (use %ld and %lf in the
 *     control string for the printf)
 *   - Create an array of eight 1-byte characters and
 *     store values in the array that will duplicate
 *     the original double value.  You may wish to use
 *     this tool:  https://tinyurl.com/ndcf235 
 *   - Copy the byte array into a new double variable 
 *     using a non-converting cast
 *   - Print the result as a double to check that 
 *     everything worked as planned
 */

void exercise_two() {
    printf("\nBegin exercise two.\n");
    double f = 3.14159265;
    long a1 = *((long*)(&f));  // non-converting cast
    long a2 = (long)f;         // converting cast
    printf("3.14159265 as double: ");    
    printBits((void*)&f,sizeof(double));
    printf("non-converting cast to long: ");    
    printBits((void*)&a1,sizeof(long));
    printf("nconverting cast to long: ");    
    printBits((void*)&a2,sizeof(long));
    printf("long1 printed as long:  %ld\n",a1);
    printf("long1 printed as double:  %lf\n",a1);
    printf("long2 printed as long:  %ld\n",a2);
    printf("long2 printed as double:  %lf\n",a2);
    printf("double printed as long:  %ld\n",f);
    printf("double printed as double:  %l10.8f\n",f);

    // assemble a value from bytes
    char arr[8];
    arr[0] = 241;
    printf("First byte: ");
    printBits((void*)(arr),sizeof(char));
    arr[1] = 212;
    printf("Second byte: ");
    printBits((void*)(arr+1),sizeof(char));
    arr[2] = 200;
    printf("Third byte: ");
    printBits((void*)(arr+2),sizeof(char));
    arr[3] = 83;
    printf("Fourth byte: ");
    printBits((void*)(arr+3),sizeof(char));
    arr[4] = 251;
    printf("Fifth byte: ");
    printBits((void*)(arr+4),sizeof(char));
    arr[5] = 33;
    printf("Sixth byte: ");
    printBits((void*)(arr+5),sizeof(char));
    arr[6] = 9;
    printf("Seventh byte: ");
    printBits((void*)(arr+6),sizeof(char));
    arr[7] = 64;
    printf("Eighth byte: ");
    printBits((void*)(arr+7),sizeof(char));
    printf("All eight bytes together: ");
    printBits((void*)arr,8*sizeof(char));
    double x = *((double*)arr);  // non-converting cast
    printf("non-converting cast to double: ");
    printBits((void*)arr,sizeof(double));    
    printf("Value printed as double:  %l10.8f\n",x);
    printf("End of exercise two.\n");
}

int main() {
    example_one();
    exercise_one();
    example_two();
    exercise_two();
}