Laboratory Work No. 1

Memory Representations of Primitive Data
In this laboratory work, you should research and find a solution (proved by C code) for each task below:
a) How many bits a byte equals to?
b) How many bytes a basic data type (char, int, float, and double) occupies in memory?
c) How many bits an integer variable occupies in memory?
d) What is the minimum and maximum value a character variable can store?
e) What is the minimum and maximum value a short-integer variable can store?
f) What is the minimum and maximum value an integer variable can store?
g) What range of values can be stored in a single-precision floating-point variable?
h) Write a C program that bitwise displays the character value ‘A’ to the screen.
i) Write a C program that bitwise displays the short-integer value 167 to the screen.
j) Write a C program that bitwise displays the single-precision-floating-point value 3.14 to the screen.
Note that you can write a few functions and/or assemble all tasks in a single C program.
Hint: There are some header files in C that can be used for answering the tasks above

a) A byte is equal to 8 bits.

b) The basic data types' memory allocations are as follows:
- char: 1 byte
- int: 4 bytes
- float: 4 bytes
- double: 8 bytes

c) An integer variable occupies 4 bytes, which is equal to 32 bits.

d) A character variable can store values from 0 to 255. The minimum value is represented by the character '\0', while the maximum value is represented by the character 255.

e) A short-integer variable can store values from -32768 to 32767.

f) An integer variable can store values from -2147483648 to 2147483647.

g) A single-precision floating-point variable can store a range of values from approximately 1.2 × 10^-38 to 3.4 × 10^38.

h) Here's a C program that bitwise displays the character value 'A' to the screen:
```c
#include <stdio.h>

void bitwiseDisplayChar(char c) {
for (int i = 7; i >= 0; i--) {
printf("%d", (c >> i) & 1);
}
printf("\n");
}

int main() {
char ch = 'A';
bitwiseDisplayChar(ch);
return 0;
}
```

i) Here's a C program that bitwise displays the short-integer value 167 to the screen:
```c
#include <stdio.h>

void bitwiseDisplayShort(int num) {
for (int i = 15; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
}

int main() {
short num = 167;
bitwiseDisplayShort(num);
return 0;
}
```

j) Here's a C program that bitwise displays the single-precision-floating-point value 3.14 to the screen:
```c
#include <stdio.h>

void bitwiseDisplayFloat(float num) {
unsigned int floatToBits = *((unsigned int*) &num);
for (int i = 31; i >= 0; i--) {
printf("%d", (floatToBits >> i) & 1);
}
printf("\n");
}

int main() {
float num = 3.14;
bitwiseDisplayFloat(num);
return 0;
}
```

Remember to include the necessary header files like `stdio.h` and `stdlib.h` for proper compilation and execution of these programs.

To complete the tasks, you can create a C program that includes the following steps for each task:

a) Calculate the number of bits in a byte:

```
#include <stdio.h>

int main() {
int bitsInByte = 8;
printf("A byte equals to %d bits\n", bitsInByte);
return 0;
}
```

b) Calculate the number of bytes occupied by each basic data type:

```
#include <stdio.h>

int main() {
printf("char occupies %zu byte(s)\n", sizeof(char));
printf("int occupies %zu byte(s)\n", sizeof(int));
printf("float occupies %zu byte(s)\n", sizeof(float));
printf("double occupies %zu byte(s)\n", sizeof(double));
return 0;
}
```

c) Calculate the number of bits occupied by an integer variable:

```
#include <stdio.h>

int main() {
int bitsInInteger = sizeof(int) * 8;
printf("An integer variable occupies %d bits\n", bitsInInteger);
return 0;
}
```

d) Find the minimum and maximum values that a character variable can store:

```
#include <stdio.h>
#include <limits.h>

int main() {
printf("The minimum value a character variable can store is %d\n", CHAR_MIN);
printf("The maximum value a character variable can store is %d\n", CHAR_MAX);
return 0;
}
```

e) Find the minimum and maximum values that a short-integer variable can store:

```
#include <stdio.h>
#include <limits.h>

int main() {
printf("The minimum value a short-integer variable can store is %d\n", SHRT_MIN);
printf("The maximum value a short-integer variable can store is %d\n", SHRT_MAX);
return 0;
}
```

f) Find the minimum and maximum values that an integer variable can store:

```
#include <stdio.h>
#include <limits.h>

int main() {
printf("The minimum value an integer variable can store is %d\n", INT_MIN);
printf("The maximum value an integer variable can store is %d\n", INT_MAX);
return 0;
}
```

g) Find the range of values that can be stored in a single-precision floating-point variable:

```
#include <stdio.h>
#include <float.h>

int main() {
printf("The range of values that can be stored in a single-precision floating-point variable is from %.10e to %.10e\n", FLT_MIN, FLT_MAX);
return 0;
}
```

h) Display the character value 'A' bitwise:

```
#include <stdio.h>

void bitwiseDisplayChar(char c) {
for (int i = 7; i >= 0; i--) {
if (c & (1 << i)) {
printf("1");
} else {
printf("0");
}
}
}

int main() {
char c = 'A';
printf("Bitwise representation of 'A': ");
bitwiseDisplayChar(c);
printf("\n");
return 0;
}
```

i) Display the short-integer value 167 bitwise:

```
#include <stdio.h>

void bitwiseDisplayShort(short s) {
for (int i = 15; i >= 0; i--) {
if (s & (1 << i)) {
printf("1");
} else {
printf("0");
}
}
}

int main() {
short s = 167;
printf("Bitwise representation of 167: ");
bitwiseDisplayShort(s);
printf("\n");
return 0;
}
```

j) Display the single-precision floating-point value 3.14 bitwise:

```
#include <stdio.h>

void bitwiseDisplayFloat(float f) {
unsigned int* ptr = (unsigned int*)&f;
unsigned int bits = *ptr;
for (int i = 31; i >= 0; i--) {
if (bits & (1u << i)) {
printf("1");
} else {
printf("0");
}
}
}

int main() {
float f = 3.14;
printf("Bitwise representation of 3.14: ");
bitwiseDisplayFloat(f);
printf("\n");
return 0;
}
```

You can save each of the above code snippets in separate C files (e.g., task_a.c, task_b.c, etc.) and compile and execute them separately to obtain the required results.

To solve the tasks in this laboratory work, you can use C programming language and some header files that provide predefined data types and functions for manipulating data. Here is an explanation of how to approach each task:

a) How many bits a byte equals to?
A byte is the smallest addressable unit of memory in most computer architectures, and it typically consists of 8 bits. You can verify this by using the `sizeof` operator in C.

b) How many bytes a basic data type (char, int, float, and double) occupies in memory?
To determine the size of each basic data type, you can use the `sizeof` operator. For example:

- `sizeof(char)` - This will give you the number of bytes occupied by a `char`.
- `sizeof(int)` - This will give you the number of bytes occupied by an `int`.
- `sizeof(float)` - This will give you the number of bytes occupied by a `float`.
- `sizeof(double)` - This will give you the number of bytes occupied by a `double`.

c) How many bits an integer variable occupies in memory?
To determine the number of bits occupied by an integer variable, you can multiply the number of bytes by 8.

d) What is the minimum and maximum value a character variable can store?
The minimum and maximum values of a character variable depend on whether it is signed or unsigned. By default, in C, `char` is signed. Signed `char` can store values from -128 to 127. Unsigned `char` can store values from 0 to 255.

e) What is the minimum and maximum value a short-integer variable can store?
The minimum and maximum values of a short-integer variable also depend on whether it is signed or unsigned. By default, in C, `short int` is signed. Signed `short int` can store values from -32,768 to 32,767. Unsigned `short int` can store values from 0 to 65,535.

f) What is the minimum and maximum value an integer variable can store?
The minimum and maximum values of an integer variable depend on whether it is signed or unsigned. By default, in C, `int` is signed. Signed `int` can store values from -2,147,483,648 to 2,147,483,647. Unsigned `int` can store values from 0 to 4,294,967,295.

g) What range of values can be stored in a single-precision floating-point variable?
The range of values that can be stored in a single-precision floating-point variable (float) depends on its size. In C, a float typically occupies 4 bytes (32 bits). It can represent a range of values from approximately 1.2E-38 to 3.4E+38, with a precision of about 6 decimal places.

h) Write a C program that bitwise displays the character value ‘A’ to the screen.
You can use the bitwise shift operator and bitwise AND operator to extract and display the individual bits of the character value 'A'. Here's an example code snippet:

```c
#include <stdio.h>

void displayBits(char value) {
for (int i = 7; i >= 0; i--) {
printf("%d", (value >> i) & 1);
}
}

int main() {
char ch = 'A';
printf("Bitwise representation of 'A': ");
displayBits(ch);
return 0;
}
```

i) Write a C program that bitwise displays the short-integer value 167 to the screen.
You can use a similar approach as in the previous task. Here's an example code snippet:

```c
#include <stdio.h>

void displayBits(short value) {
for (int i = 15; i >= 0; i--) {
printf("%d", (value >> i) & 1);
}
}

int main() {
short num = 167;
printf("Bitwise representation of 167: ");
displayBits(num);
return 0;
}
```

j) Write a C program that bitwise displays the single-precision-floating-point value 3.14 to the screen.
To display the bitwise representation of a float value, you can use a union to store the float as an unsigned integer and then extract and display the individual bits of the integer. Here's an example code snippet:

```c
#include <stdio.h>

union FloatInt {
float floatValue;
unsigned int intValue;
};

void displayBits(unsigned int value) {
for (int i = 31; i >= 0; i--) {
printf("%d", (value >> i) & 1);
}
}

int main() {
union FloatInt num;
num.floatValue = 3.14;
printf("Bitwise representation of 3.14: ");
displayBits(num.intValue);
return 0;
}
```

These are the steps to solve each task in the given laboratory work. Remember to include the necessary header files (`stdio.h` for input/output operations) at the beginning of your C program.