Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intro-comparch/number-representation: Add reading and tasks #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bitmask
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>

Check failure on line 1 in chapters/intro-computer-architecture/binary-hex/demos/bitmask/bitmask.c

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1

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;
}
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.
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/odd_even
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/odd_even
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>

Check failure on line 1 in chapters/intro-computer-architecture/binary-hex/drills/tasks/odd-even/support/odd_even.c

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1
#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;
}

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/rotations
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/rotations
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

Check failure on line 1 in chapters/intro-computer-architecture/binary-hex/drills/tasks/rotations/support/rotations.c

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1

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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading