From 319f308b49786c9fcd3a77aa4f234df08eb2fc83 Mon Sep 17 00:00:00 2001 From: stderr64 Date: Fri, 6 Feb 2026 21:53:12 +0200 Subject: Added experiment showing how to detect is integer positive or negative --- bit_test/Makefile | 5 ++++ bit_test/test_positive_int.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 bit_test/Makefile create mode 100644 bit_test/test_positive_int.c (limited to 'bit_test') 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 +#include +#include +#include +#include +#include + +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 ); +} -- cgit v1.2.3-96-g3da7