-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
intro-comparch/number-representation: Add reading and tasks
- Add reading material and tasks for the "Number Representation" topic - Remove template chapter - Modify `config.yaml` to render the new content and comment out mentions to slides for future use
- Loading branch information
Showing
73 changed files
with
921 additions
and
513 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
chapters/intro-computer-architecture/binary-hex/demos/bitmask/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bitmask |
22 changes: 22 additions & 0 deletions
22
chapters/intro-computer-architecture/binary-hex/demos/bitmask/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CC = gcc | ||
CFLAGS = -Wall | ||
LDFLAGS = | ||
|
||
TARGET_EXEC = bitmask | ||
|
||
SRCS := $(shell find $(SRC_DIRS) -name "*.c") | ||
OBJS := $(SRCS:.c=.o) | ||
|
||
$(info OBJS is $(OBJS)) | ||
$(info SRCS is $(SRCS)) | ||
|
||
$(TARGET_EXEC): $(OBJS) | ||
$(CC) $(OBJS) -o $@ $(LDFLAGS) | ||
|
||
.o: %.c | ||
$(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@ | ||
|
||
.PHONY: clean | ||
|
||
clean: | ||
$(RM) -r *.o $(TARGET_EXEC) |
24 changes: 24 additions & 0 deletions
24
chapters/intro-computer-architecture/binary-hex/demos/bitmask/bitmask.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <stdio.h> | ||
|
||
static unsigned int v = 0x1827921; | ||
|
||
static unsigned int what(unsigned int num) | ||
{ | ||
size_t i; | ||
unsigned int m = 0x1; | ||
unsigned int count = 0; | ||
|
||
for (i = 0; i < 8 * (sizeof(num)); i++) { | ||
if (m & num) | ||
count++; | ||
m <<= 1; | ||
} | ||
|
||
return count; | ||
} | ||
|
||
int main(void) | ||
{ | ||
printf("value: %u, what: %u\n", v, what(v)); | ||
return 0; | ||
} |
25 changes: 25 additions & 0 deletions
25
chapters/intro-computer-architecture/binary-hex/drills/tasks/conversions/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Conversions | ||
|
||
Perform the following conversions between numbering systems: | ||
|
||
a. From decimal to binary and hexadecimal: | ||
|
||
- `121` | ||
- `18446` | ||
|
||
b. Convert to decimal: | ||
|
||
- `0b1100010111010010` | ||
- `0xBB29` | ||
|
||
c. From hexadecimal to binary: | ||
|
||
- `0x5E` | ||
- `0x4A01` | ||
|
||
d. From binary to hexadecimal: | ||
|
||
- `0b01111101` | ||
- `0b1000110000011111` | ||
|
||
If you're having difficulties solving this exercise, go through [this](../../../reading/README.md#conversion-between-binary-and-hexadecimal) reading material. |
19 changes: 19 additions & 0 deletions
19
chapters/intro-computer-architecture/binary-hex/drills/tasks/odd-even/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Binary Even and Hexadecimal Odd | ||
|
||
You will solve the exercise starting from the file `odd_even.c` located in the directory `drills/tasks/odd-even/`. | ||
|
||
Traverse an array of 32-bit integers using pointer operations and display the even numbers in binary and the odd numbers in hexadecimal. | ||
|
||
>**NOTE**: Use bitwise operations wherever possible in your solution! | ||
>**NOTE**: For the array `[214, 71, 84, 134, 86]`, the program will display: | ||
> | ||
> ```text | ||
> 0b11010110 | ||
> 0x00000047 | ||
> 0b01010100 | ||
> 0b10000110 | ||
> 0b01010110 | ||
> ``` | ||
If you're having difficulties solving this exercise, go through [this](../../../reading/README.md#logical-shifts) reading material. |
1 change: 1 addition & 0 deletions
1
chapters/intro-computer-architecture/binary-hex/drills/tasks/odd-even/solution/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/odd_even |
1 change: 1 addition & 0 deletions
1
chapters/intro-computer-architecture/binary-hex/drills/tasks/odd-even/solution/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../support/Makefile |
67 changes: 67 additions & 0 deletions
67
chapters/intro-computer-architecture/binary-hex/drills/tasks/odd-even/solution/odd_even.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
|
||
void print_binary(int number, int nr_bits) | ||
{ | ||
int i; | ||
char *bits = malloc(sizeof(*bits) * nr_bits); | ||
|
||
if (bits == NULL) { | ||
perror("malloc() failed while allocating `bits`"); | ||
exit(errno); | ||
} | ||
|
||
for (i = 0; i < nr_bits; ++i) { | ||
*(bits + i) = 1 & number; | ||
number >>= 1; | ||
} | ||
|
||
printf("0b"); | ||
for (i = nr_bits - 1; i >= 0; --i) | ||
printf("%d", *(bits + i)); | ||
printf("\n"); | ||
|
||
free(bits); | ||
} | ||
|
||
void check_parity(int *numbers, int n) | ||
{ | ||
int i, curr_nr; | ||
|
||
for (i = 0; i < n; ++i) { | ||
curr_nr = *(numbers + i); | ||
printf("%5d: ", curr_nr); | ||
if (curr_nr & 1) | ||
printf("0x%08X\n", curr_nr); | ||
else | ||
print_binary(curr_nr, 8); | ||
} | ||
} | ||
|
||
int main(void) | ||
{ | ||
int *numbers, i, n; | ||
|
||
printf("Size of array: "); scanf("%d", &n); | ||
|
||
numbers = malloc(sizeof(*numbers) * n); | ||
|
||
if (numbers == NULL) { | ||
perror("malloc() failed while allocating `numbers`"); | ||
exit(errno); | ||
} | ||
|
||
for (i = 0; i < n; ++i) { | ||
printf("Number %d: ", i + 1); | ||
scanf("%d", numbers + i); | ||
} | ||
|
||
check_parity(numbers, n); | ||
|
||
free(numbers); | ||
|
||
return EXIT_SUCCESS; | ||
} |
1 change: 1 addition & 0 deletions
1
chapters/intro-computer-architecture/binary-hex/drills/tasks/odd-even/support/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/odd_even |
22 changes: 22 additions & 0 deletions
22
chapters/intro-computer-architecture/binary-hex/drills/tasks/odd-even/support/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CC = gcc | ||
CFLAGS = -Wall | ||
LDFLAGS = | ||
|
||
TARGET_EXEC = odd_even | ||
|
||
SRCS := $(shell find $(SRC_DIRS) -name "*.c") | ||
OBJS := $(SRCS:.c=.o) | ||
|
||
$(info OBJS is $(OBJS)) | ||
$(info SRCS is $(SRCS)) | ||
|
||
$(TARGET_EXEC): $(OBJS) | ||
$(CC) $(OBJS) -o $@ $(LDFLAGS) | ||
|
||
.o: %.c | ||
$(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@ | ||
|
||
.PHONY: clean | ||
|
||
clean: | ||
$(RM) -r *.o $(TARGET_EXEC) |
24 changes: 24 additions & 0 deletions
24
chapters/intro-computer-architecture/binary-hex/drills/tasks/odd-even/support/odd_even.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
void print_binary(int number, int nr_bits) | ||
{ | ||
/* TODO */ | ||
(void) number; | ||
(void) nr_bits; | ||
} | ||
|
||
void check_parity(int *numbers, int n) | ||
{ | ||
/* TODO */ | ||
(void) numbers; | ||
(void) n; | ||
} | ||
|
||
int main(void) | ||
{ | ||
/* TODO: Test functions */ | ||
|
||
return 0; | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
chapters/intro-computer-architecture/binary-hex/drills/tasks/rotations/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Rotations | ||
|
||
You will solve the exercise starting from the file `rotations.c` located in the directory `drills/tasks/odd-even/`. | ||
|
||
Implement left and right rotations for 32-bit integers in C. | ||
|
||
> **TIP**: The rotation operation (also known as circular shift) is similar to the shift operation, with the only difference being that the empty space generated by the shift is filled with the discarded bit. | ||
> | ||
> Example of **left** rotation by **one** bit: | ||
> | ||
> ![Left Logical Rotation](../../../media/left-logical-rotation.svg) | ||
> **NOTE**: | ||
> | ||
> ```c | ||
> rotate_left(0x80000000, 1) = 1 | ||
> rotate_right(0x00000001, 16) = 65536 | ||
> ``` | ||
If you're having difficulties solving this exercise, go through [this](../../../reading/README.md#logical-shifts) reading material. |
1 change: 1 addition & 0 deletions
1
chapters/intro-computer-architecture/binary-hex/drills/tasks/rotations/solution/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/rotations |
1 change: 1 addition & 0 deletions
1
chapters/intro-computer-architecture/binary-hex/drills/tasks/rotations/solution/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../support/Makefile |
41 changes: 41 additions & 0 deletions
41
chapters/intro-computer-architecture/binary-hex/drills/tasks/rotations/solution/rotations.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <stdio.h> | ||
|
||
void rotate_left(int *number, int bits) | ||
{ | ||
unsigned int bit_mask = -1; | ||
|
||
bit_mask <<= (sizeof(*number) * 8 - bits); | ||
bit_mask &= (*number); | ||
bit_mask >>= (sizeof(*number) * 8 - bits); | ||
(*number) <<= bits; | ||
(*number) |= bit_mask; | ||
} | ||
|
||
void rotate_right(int *number, int bits) | ||
{ | ||
unsigned int bit_mask = -1; | ||
|
||
bit_mask >>= (sizeof(*number) * 8 - bits); | ||
bit_mask &= (*number); | ||
bit_mask <<= (sizeof(*number) * 8 - bits); | ||
(*number) >>= bits; | ||
(*number) |= bit_mask; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int number; | ||
|
||
number = 0x80000000; | ||
rotate_left(&number, 1); | ||
printf("After left rotation: %d\n", number); | ||
|
||
number = 0x00000001; | ||
rotate_right(&number, 16); | ||
printf("After right rotation: %d\n", number); | ||
|
||
|
||
return 0; | ||
} |
1 change: 1 addition & 0 deletions
1
chapters/intro-computer-architecture/binary-hex/drills/tasks/rotations/support/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/rotations |
22 changes: 22 additions & 0 deletions
22
chapters/intro-computer-architecture/binary-hex/drills/tasks/rotations/support/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CC = gcc | ||
CFLAGS = -Wall | ||
LDFLAGS = | ||
|
||
TARGET_EXEC = rotations | ||
|
||
SRCS := $(shell find $(SRC_DIRS) -name "*.c") | ||
OBJS := $(SRCS:.c=.o) | ||
|
||
$(info OBJS is $(OBJS)) | ||
$(info SRCS is $(SRCS)) | ||
|
||
$(TARGET_EXEC): $(OBJS) | ||
$(CC) $(OBJS) -o $@ $(LDFLAGS) | ||
|
||
.o: %.c | ||
$(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@ | ||
|
||
.PHONY: clean | ||
|
||
clean: | ||
$(RM) -r *.o $(TARGET_EXEC) |
23 changes: 23 additions & 0 deletions
23
chapters/intro-computer-architecture/binary-hex/drills/tasks/rotations/support/rotations.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
|
||
void rotate_left(int *number, int bits) | ||
{ | ||
/* TODO */ | ||
(void) number; | ||
(void) bits; | ||
} | ||
|
||
void rotate_right(int *number, int bits) | ||
{ | ||
/* TODO */ | ||
(void) number; | ||
(void) bits; | ||
} | ||
|
||
int main(void) | ||
{ | ||
/* TODO: Test functions */ | ||
|
||
return 0; | ||
} | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions
4
chapters/intro-computer-architecture/binary-hex/media/left-logical-rotation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions
4
chapters/intro-computer-architecture/binary-hex/media/left-logical-shift.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions
4
chapters/intro-computer-architecture/binary-hex/media/right-logical-shift.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.