From 6073a1f17c90381587948db5382089e2bbb2eab3 Mon Sep 17 00:00:00 2001 From: Muhammad Salah <67175615+MuhammadSalah-MS@users.noreply.github.com> Date: Sat, 14 Jun 2025 16:48:29 +0300 Subject: [PATCH] Update setbits.c follows the bit manipulation style (p as MSB of the field) as demonstrated in K&R, particularly aligning with the getbits example (p+1-n). So bits to be set are the least after the position p. --- chapter_2/exercise_2_06/setbits.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter_2/exercise_2_06/setbits.c b/chapter_2/exercise_2_06/setbits.c index b98b680..93d9c9c 100644 --- a/chapter_2/exercise_2_06/setbits.c +++ b/chapter_2/exercise_2_06/setbits.c @@ -28,8 +28,8 @@ unsigned int setbits(int x, int p, int n, int y) { ++p; // First position is 0 - unsigned int mask1 = (~(~(~0 << n) << p) & x); - unsigned int mask2 = (~(~0 << n) & y) << p; + unsigned int mask1 = (~(~(~0 << n) << (p-n)) & x); + unsigned int mask2 = (~(~0 << n) & y) << (p-n); return mask1 | mask2; }