C checking for a signed number

The next way to verify the correctness of checking the number of signed numbers for binary files on my machine is correct, but I do not have such add-ons or iconic machines for checking. Will the code work correctly and, more importantly, is it portable?

File: platform.h

#ifndef PLATFORM_H #define PLATFORM_H #include <limits.h> static const union { signed char sc; unsigned char uc; } plat_4xvYw = {.sc = -1}; #define IS_TWOS_COMPL (plat_4xvYw.uc == UCHAR_MAX) #define IS_ONES_COMPL (plat_4xvYw.uc == UCHAR_MAX - 1) #define IS_SIGNED_MAG (plat_4xvYw.uc == (1U << (CHAR_BIT - 1)) + 1U) #endif 

File: ac

 #include <inttypes.h> #include <limits.h> #include "platform.h" #include <assert.h> int main (void) { assert (IS_TWOS_COMPL); if (IS_TWOS_COMPL) { printf ("twos complement\n"); } else if (IS_ONES_COMPL) { printf ("ones complement\n"); } else if (IS_SIGNED_MAG) { printf ("signed magnitude\n"); } return 0; } 
+4
source share
1 answer

I think you better just mask the bit of the negative int :

 if ((-1 & 0x1) == 0) { // -1 ends in "0" => 1s' complement } else if ((-1 & 0x2) == 0) { // -1 ends in "01" => sign-magnitude } else { // -1 ends in "11" => two complement } 

Strictly speaking, this does not tell you the same thing as your code, since there is no guarantee that int and signed char use the same sign bit value. But (a) seriously? and (b) it works for int types and more, for smaller types it is more complicated. unsigned char guaranteed to have no fill bits, but signed char not. Therefore, I consider it legal to have (for example) CHAR_BIT == 9 , UCHAR_MAX = 511 , CHAR_MAX = 127 , and signed char - 1 bit of filling. Then your code may fail: the sign bit in the stored value with the sign is not necessarily where you expect it to be, and the fill bit value can be either 0 or 1.

In many cases, you can simply use int8_t in the program instead of a signed char . It is guaranteed to be 2 extras if it exists, so it can save you from worrying about the signed char representation. If it does not exist, the program will not compile, but it is the same as assert . You will get a false negative result from platforms that are 2 add-ons but do not have an 8-bit char and therefore do not provide int8_t . It may or may not bother you ...

+4
source

Source: https://habr.com/ru/post/1380379/


All Articles