summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstderr64 <stderr64@null.net>2026-02-06 21:53:12 +0200
committerstderr64 <stderr64@null.net>2026-02-06 21:53:12 +0200
commit319f308b49786c9fcd3a77aa4f234df08eb2fc83 (patch)
treec74a0d8d88594cc5ca71773d8a3fda3861b5186b
parent1b7cac8b58a437acbfe6a8d84658cf42c823fd78 (diff)
downloadexperiments-master.tar.gz
experiments-master.tar.zst
Added experiment showing how to detect is integer positive or negativeHEADmaster
-rw-r--r--bit_test/Makefile5
-rw-r--r--bit_test/test_positive_int.c55
2 files changed, 60 insertions, 0 deletions
diff --git a/bit_test/Makefile b/bit_test/Makefile
new file mode 100644
index 0000000..d62bb86
--- /dev/null
+++ b/bit_test/Makefile
@@ -0,0 +1,5 @@
+CC=gcc
+CFLAGS=-O2 -mavx2 -Wall -Werror -pedantic -std=gnu23
+
+test_positive_int:
+ $(CC) ${CFLAGS} ./test_positive_int.c -o ./test_positive_int
diff --git a/bit_test/test_positive_int.c b/bit_test/test_positive_int.c
new file mode 100644
index 0000000..03d1bd0
--- /dev/null
+++ b/bit_test/test_positive_int.c
@@ -0,0 +1,55 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+
+bool is_int_positive( int test_int ){
+ /*
+ multiply test_int by 2 to get the amount of characters/bits
+ that would be in the hexadecimal value.
+ For example:
+ 8-bit unsigned/signed = 1 * 2 = 2 (0x00 - 0xFF)
+ 16-bit unsigned/signed = 2 * 2 = 4 (0x0000 - 0xFFFF)
+ 32-bit unsigned/signed = 4 * 2 = 8 (0x00000000 - 0xFFFFFFFF)
+ 64-bit unsigned/signed = 8 * 2 = 16 (0x0000000000000000 - 0xFFFFFFFFFFFFFFFF)
+ */
+ size_t value_bits = sizeof(test_int) * 2;
+ /*
+ Next check if first bit is 1 (positive integer) or 0 (negative integer).
+ To do this the value bits must be used to right shift by the value bits count
+ so only the MSB is left.
+ */
+ size_t value_bits_shift = test_int >> (value_bits - 1);
+ /*
+ Now do bitwise and operation to compare if the MSB is 1 (negative) or 0 (positive).
+ This works because the MSB tells if the integer contained in following bits should be taken
+ as positive or negative integer.
+ For positive integers the MSB is 0 and for negative integers the MSB is set to 1.
+ Because value_bits_shift was shifted right by value_bits minus 1 the result is
+ being left with just the bit containing the MSB value.
+ The bitwise and below checks if the MSB is not 1 then return true (positive integer) and otherwise false (negative integer).
+ */
+ return (!(value_bits_shift & 0x1)) ? true : false;
+}
+
+int main( int argc, char **args ){
+ // Positive integer
+ int first_int = 12345;
+ // Negative integer
+ int second_int = -12345;
+ // Third integer that is just under 0
+ int third_int = -1;
+ // Fourth integer to test
+ int fourth_int = 1;
+ // Check the first integer
+ fprintf( stdout, "Result for positive integer: %i\n", is_int_positive(first_int) );
+ // Now do the same for the second one
+ fprintf( stdout, "Result for negative integer: %i\n", is_int_positive(second_int) );
+ // Third integer
+ fprintf( stdout, "Result for third integer that is -1: %i\n", is_int_positive(third_int) );
+ // Fourth integer
+ fprintf( stdout, "Result for fourth integer that is just positive by one: %i\n", is_int_positive(fourth_int) );
+ exit( EXIT_SUCCESS );
+}