Skip to content
Open
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
65 changes: 32 additions & 33 deletions chapter_2/exercise_2_08/rightrot.c
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
#include <math.h>
#include <limits.h>
#include <stdio.h>

void printbin(unsigned int x);
unsigned int rightrot(unsigned int x, unsigned int n);
/* Exercise 2.8: Write a function rightrot(x, n) that returns the value
* of the integer x rotated to the right by n bit positions.
*/

int main(void) {
// Using hexadecimal instead of binary literals (0b...) for C89/C99
// compatibility 0xF5 = 0b11110101
unsigned int x = 0xF5;
void to_binary(unsigned short n) {
unsigned long i = 0;
int binary[32];
printf("0b");

printbin(x);
printbin(rightrot(x, 5));
if (n == 0) {
printf("0\n");
return;
}

return 0;
while (i < sizeof(unsigned short) * CHAR_BIT) {
binary[i++] = n % 2;
n = n / 2;
}
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}

void printbin(unsigned int x) {
unsigned int n = sizeof(unsigned int);
unsigned short rightrot(unsigned short x, int n) {
unsigned w = sizeof(unsigned short) * CHAR_BIT;
n %= w;

printf("0b");
return (x >> n) | (x << (w - n));
}
Comment on lines +28 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Undefined behavior when n is 0.

When n is 0 (or a multiple of w), line 32 evaluates to (x >> 0) | (x << w). Shifting by the bit width or more is undefined behavior in C.

Apply this diff to fix the issue:

 unsigned short rightrot(unsigned short x, int n) {
     unsigned w = sizeof(unsigned short) * CHAR_BIT;
     n %= w;
-
+    if (n == 0)
+        return x;
     return (x >> n) | (x << (w - n));
 }
🤖 Prompt for AI Agents
In chapter_2/exercise_2_08/rightrot.c around lines 28 to 33, the code does a
left shift by (w - n) which becomes a shift by w when n is 0 (or a multiple of
w) causing undefined behavior; fix by after computing n %= w, check if n == 0
and simply return x to avoid any shift by w, otherwise perform the existing
rotate expression.


int i;
for (i = n * 8 - 1; i >= 0; --i) {
(x & (unsigned int)pow(2, i)) ? putchar('1') : putchar('0');
}
int main(void) {
unsigned short x;
x = 55;

putchar('\n');
}
printf("x = %d -->", x);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Use correct format specifier for unsigned short.

The format specifier %d is for signed integers. For unsigned short, use %hu or cast to unsigned and use %u.

Apply this diff:

-    printf("x = %d -->", x);
+    printf("x = %hu -->", x);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
printf("x = %d -->", x);
printf("x = %hu -->", x);
🤖 Prompt for AI Agents
In chapter_2/exercise_2_08/rightrot.c around line 39, the printf uses the signed
integer specifier "%d" for an unsigned short variable x; change the format
specifier to "%hu" (or cast x to unsigned and use "%u") so the value prints
correctly, and update the printf call to use the chosen unsigned specifier.

to_binary(x);

unsigned int rightrot(unsigned int x, unsigned int n) {
unsigned int msb_1 = ~(~(unsigned)0 >> 1);

// Using unsigned int for i to match the unsigned n parameter
// This prevents sign comparison warnings
unsigned int i;
for (i = 0; i < n; ++i) {
if (x & 1) {
x = (x >> 1) | msb_1;
} else {
x = (x >> 1);
}
}
to_binary(rightrot(x, 3));

return x;
return 0;
}

// NOTE: The rightrot function rotate the entire unsigned int var and if we
Expand Down