aboutsummaryrefslogtreecommitdiff
path: root/bits.c
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2025-09-24 13:50:55 +0800
committerRichard Qin <richard.qin.x@gmail.com>2025-09-24 13:50:55 +0800
commit87f0114dd8d2cf355c4e631ab089ddd75abdcc0c (patch)
treeddb6753774ee91f090f59a89adf7e21f780dc2ad /bits.c
downloaddatalab2026-87f0114dd8d2cf355c4e631ab089ddd75abdcc0c.tar.gz
datalab2026-87f0114dd8d2cf355c4e631ab089ddd75abdcc0c.tar.xz
Initial commit: Add starter code for Datalab
Diffstat (limited to 'bits.c')
-rw-r--r--bits.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/bits.c b/bits.c
new file mode 100644
index 0000000..09baed2
--- /dev/null
+++ b/bits.c
@@ -0,0 +1,177 @@
+/* WARNING: Do not include any other libraries here,
+ * otherwise you will get an error while running test.py
+ * You can still use printf for debugging without including
+ * <stdio.h>, although you might get a compiler warning. In general,
+ * it's not good practice to ignore compiler warnings, but in this
+ * case it's OK.
+ *
+ * Using printf will interfere with our script capturing the execution results.
+ * At this point, you can only test correctness with ./btest.
+ * After confirming everything is correct in ./btest, remove the printf
+ * and run the complete tests with test.py.
+ */
+
+ /*
+ * bitAnd - x & y using only ~ and |
+ * Example: bitAnd(4, 5) = 4
+ * Legal ops: ~ |
+ * Max ops: 7
+ * Difficulty: 1
+ */
+int bitAnd(int x, int y) {
+ return 2;
+}
+
+/*
+ * bitXor - x ^ y using only ~ and &
+ * Example: bitXor(4, 5) = 1
+ * Legal ops: ~ &
+ * Max ops: 7
+ * Difficulty: 1
+ */
+int bitXor(int x, int y) {
+ return 2;
+}
+
+/*
+ * samesign - Determines if two integers have the same sign.
+ * 0 is not positive, nor negative
+ * Example: samesign(0, 1) = 0, samesign(0, 0) = 1
+ * samesign(-4, -5) = 1, samesign(-4, 5) = 0
+ * Legal ops: >> << ! ^ && if else &
+ * Max ops: 12
+ * Difficulty: 2
+ *
+ * Parameters:
+ * x - The first integer.
+ * y - The second integer.
+ *
+ * Returns:
+ * 1 if x and y have the same sign , 0 otherwise.
+ */
+int samesign(int x, int y) {
+ return 2;
+}
+
+/*
+ * logtwo - Calculate the base-2 logarithm of a positive integer using bit
+ * shifting. (Think about bitCount)
+ * Note: You may assume that v > 0
+ * Example: logtwo(32) = 5
+ * Legal ops: > < >> << |
+ * Max ops: 25
+ * Difficulty: 4
+ */
+int logtwo(int v) {
+ return 2;
+}
+
+/*
+ * byteSwap - swaps the nth byte and the mth byte
+ * Examples: byteSwap(0x12345678, 1, 3) = 0x56341278
+ * byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD
+ * Note: You may assume that 0 <= n <= 3, 0 <= m <= 3
+ * Legal ops: ! ~ & ^ | + << >>
+ * Max ops: 17
+ * Difficulty: 2
+ */
+int byteSwap(int x, int n, int m) {
+ return 2;
+}
+
+/*
+ * reverse - Reverse the bit order of a 32-bit unsigned integer.
+ * Example: reverse(0xFFFF0000) = 0x0000FFFF reverse(0x80000000)=0x1 reverse(0xA0000000)=0x5
+ * Note: You may assume that an unsigned integer is 32 bits long.
+ * Legal ops: << | & - + >> for while ! ~ (You can define unsigned in this function)
+ * Max ops: 30
+ * Difficulty: 3
+ */
+unsigned reverse(unsigned v) {
+ return 2;
+}
+
+/*
+ * logicalShift - shift x to the right by n, using a logical shift
+ * Examples: logicalShift(0x87654321,4) = 0x08765432
+ * Note: You can assume that 0 <= n <= 31
+ * Legal ops: ! ~ & ^ | + << >>
+ * Max ops: 20
+ * Difficulty: 3
+ */
+int logicalShift(int x, int n) {
+ return 2;
+}
+
+/*
+ * leftBitCount - returns count of number of consective 1's in left-hand (most) end of word.
+ * Examples: leftBitCount(-1) = 32, leftBitCount(0xFFF0F0F0) = 12,
+ * leftBitCount(0xFE00FF0F) = 7
+ * Legal ops: ! ~ & ^ | + << >>
+ * Max ops: 50
+ * Difficulty: 4
+ */
+int leftBitCount(int x) {
+ return 2;
+}
+
+/*
+ * float_i2f - Return bit-level equivalent of expression (float) x
+ * Result is returned as unsigned int, but it is to be interpreted as
+ * the bit-level representation of a single-precision floating point values.
+ * Legal ops: if else while for & | ~ + - >> << < > ! ==
+ * Max ops: 30
+ * Difficulty: 4
+ */
+unsigned float_i2f(int x) {
+ return 2;
+}
+
+/*
+ * floatScale2 - Return bit-level equivalent of expression 2*f for
+ * floating point argument f.
+ * Both the argument and result are passed as unsigned int's, but
+ * they are to be interpreted as the bit-level representation of
+ * single-precision floating point values.
+ * When argument is NaN, return argument
+ * Legal ops: & >> << | if > < >= <= ! ~ else + ==
+ * Max ops: 30
+ * Difficulty: 4
+ */
+unsigned floatScale2(unsigned uf) {
+ return 2;
+}
+
+/*
+ * float64_f2i - Convert a 64-bit IEEE 754 floating-point number to a 32-bit signed integer.
+ * The conversion rounds towards zero.
+ * Note: Assumes IEEE 754 representation and standard two's complement integer format.
+ * Parameters:
+ * uf1 - The lower 32 bits of the 64-bit floating-point number.
+ * uf2 - The higher 32 bits of the 64-bit floating-point number.
+ * Returns:
+ * The converted integer value, or 0x80000000 on overflow, or 0 on underflow.
+ * Legal ops: >> << | & ~ ! + - > < >= <= if else
+ * Max ops: 60
+ * Difficulty: 3
+ */
+int float64_f2i(unsigned uf1, unsigned uf2) {
+ return 2;
+}
+
+/*
+ * floatPower2 - Return bit-level equivalent of the expression 2.0^x
+ * (2.0 raised to the power x) for any 32-bit integer x.
+ *
+ * The unsigned value that is returned should have the identical bit
+ * representation as the single-precision floating-point number 2.0^x.
+ * If the result is too small to be represented as a denorm, return
+ * 0. If too large, return +INF.
+ *
+ * Legal ops: < > <= >= << >> + - & | ~ ! if else &&
+ * Max ops: 30
+ * Difficulty: 4
+ */
+unsigned floatPower2(int x) {
+ return 2;
+}