-
-
Notifications
You must be signed in to change notification settings - Fork 141
Implemented 16-bit version of the code for more flexibility #99
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA single exercise file is refactored to change the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5–10 minutes
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
chapter_2/exercise_2_08/rightrot.c (1)
10-10: Consider reducing the array size.The
binaryarray is sized at 32, butunsigned shorttypically uses 16 bits. While this is safe, it wastes stack space.Apply this diff to optimize the array size:
- int binary[32]; + int binary[sizeof(unsigned short) * CHAR_BIT];
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
chapter_2/exercise_2_08/rightrot.c(1 hunks)
🧰 Additional context used
🪛 Cppcheck (2.18.0)
chapter_2/exercise_2_08/rightrot.c
[warning] 23-23: Uninitialized variable
(uninitvar)
🔇 Additional comments (2)
chapter_2/exercise_2_08/rightrot.c (2)
1-1: LGTM!The change from
math.htolimits.his correct, asCHAR_BITis defined inlimits.h.
22-24: False positive from static analysis.The static analysis warning about an uninitialized variable at line 23 is a false positive. The preceding while loop (lines 18-21) initializes all elements of
binaryfrom index 0 toi-1, and the for loop only accesses those initialized indices.
| 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)); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
|
||
| putchar('\n'); | ||
| } | ||
| printf("x = %d -->", x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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.
|
@kkaname Great to see you contributing again! Can you ensure first the @coderabbitai issues are addressed first before I take a look? |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 674 🏁 Script executed: Length of output: 9828 🏁 Script executed: Length of output: 2600 🏁 Script executed: Length of output: 2797 @ohkimur Here's a summary of the issues raised by CodeRabbit that need to be addressed: Pre-merge Check Warnings
Actionable Code Issues
Nitpick
The most critical issue is #1 (undefined behavior), which must be fixed. Issues #2 and the title mismatch should also be addressed before merge. |
|
@cooderabbitai I thought I disabled |
|
@ohkimur ok its my mistake that i put 32 bit instead of saying it to be 16 since i used unsigned short type. |
I have implemented much simpler version and also 32-bit for more code flexibility
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.