From 991bede3166d9087f8c876ee6e5f937f66462bf2 Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Fri, 8 Mar 2024 23:53:54 -0500 Subject: [PATCH] finihsed --- bit_utils.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/bit_utils.c b/bit_utils.c index 1f6eec1..905bce4 100644 --- a/bit_utils.c +++ b/bit_utils.c @@ -1,4 +1,4 @@ -// PID: 9DigitPidNoSpacesOrDashes +// PID: 730677144 // I pledge the COMP 211 honor code. /** @@ -11,28 +11,37 @@ #include "bit_utils.h" unsigned int mask(unsigned int num, unsigned int bits) { - // TODO: implement - return 0; + return num & bits; } unsigned int set(unsigned int num, unsigned int bits) { - // TODO: implement - return 0; + return num | bits; } unsigned int inverse(unsigned int num, unsigned int bits) { - // TODO: implement - return 0; + return num ^ bits; } unsigned int bit_select(unsigned int num, unsigned int startbit, unsigned int endbit) { - // TODO: implement - return 0; + unsigned int shifted = num >> endbit; + unsigned int bit_count = startbit - endbit + 1; + unsigned int mask = (1 << bit_count) - 1; + return shifted & mask; } unsigned int barrel_shift(unsigned int num, unsigned int shamt) { - // TODO: implement - return 0; + const unsigned int bit_width = sizeof(unsigned int) * 8; + + shamt %= bit_width; + + if (shamt == 0) { + return num; + } + unsigned int right_part = num >> shamt; + + unsigned int left_part = num << (bit_width - shamt); + + return right_part | left_part; }