aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-09-20 23:26:28 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-09-20 23:26:28 +0800
commit8dc87e8d58730cd93b1da80f02863398ff8e2dae (patch)
treeb98138edeb014778aaae51939f379def1152c97f
parent1a0f57746d185684551358a35bb6d58b6b575810 (diff)
downloaddatalab2026-8dc87e8d58730cd93b1da80f02863398ff8e2dae.tar.gz
datalab2026-8dc87e8d58730cd93b1da80f02863398ff8e2dae.tar.xz
byteSwap
-rw-r--r--bits.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/bits.c b/bits.c
index 8652b1a..0be410f 100644
--- a/bits.c
+++ b/bits.c
@@ -101,7 +101,19 @@ int logtwo(int v) {
* Difficulty: 2
*/
int byteSwap(int x, int n, int m) {
- return 2;
+ int ns = (n << 3) ;
+ int ms = (m << 3);
+ int nb = (x >> ns) & 0xFF;
+ int mb = (x >> ms) & 0xFF;
+
+ int mask = ~((0xFF << ns) | (0xFF << ms));
+
+ x = x & mask;
+
+ x = x | (nb << ms);
+ x = x | (mb << ns);
+
+ return x;
}
/*