-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)); | ||||||
| } | ||||||
|
|
||||||
| 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); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use correct format specifier for unsigned short. The format specifier Apply this diff: - printf("x = %d -->", x);
+ printf("x = %hu -->", x);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| 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 | ||||||
|
|
||||||
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
nis 0 (or a multiple ofw), 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