#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.
 * 
 *  Note that the byte order runs backwards on some computer
 *  designs, so that to assemble a multi-byte value into a
 *  single bitstring you would reverse the order of the bytes.
 *  (So the last 8-bit segment would be first, and the first 
 *  last, etc.)
 *  
 *  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");
}

/* 
 *  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");
    // add code here...
    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 a long using a non-converting 
 *     cast
 *   - Copy its value to a second long using a converting 
 *     cast
 *   - Print all three 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");
    // add code here...
    printf("End of exercise two.\n");
}

int main() {
    example_one();
    exercise_one();
    example_two();
    exercise_two();
}