Preprocessor and Casting

1
2
3
4
5
6
7
#define zxUCHAR_MIN ((unsigned char)0u)
#define zxUCHAR_MAX (~zxUCHAR_MIN)
#if zxUCHAR_MAX == 0x7F
#define zxCHAR_BIT 7u
#elif zxUCHAR_MAX == 0xFF
#define zxCHAR_BIT 8u
#endif 

This is just throwing errors, is there anyway to perform what I'm trying to do?
1
2
3
4
5
#include <limits>

constexpr unsigned char zxUCHAR_MIN = std::numeric_limits<unsigned char>::min() ;
constexpr unsigned char zxUCHAR_MAX = std::numeric_limits<unsigned char>::max() ;
constexpr std::size_t zxCHAR_BIT = std::numeric_limits<unsigned char>::digits ;
Should've mentioned that I'm looking for a c89/c90 example, reason I'm trying is because I want to remove reliance on the original header if I can.
1
2
3
4
5
#include <limits.h>

#define zxUCHAR_MIN ((unsigned char)0u)
#define zxUCHAR_MAX UCHAR_MAX
#define zxCHAR_BIT CHAR_BIT 

You didn't read my 2nd post fully did you? I'm already aware of the <limits.h> but I was trying to remove reliance on that (will add define as well to prevent issues).
Well I have a temporary solution for now:
1
2
3
4
5
6
#elif ~0u <= 0xffffffffU
#define UI4_MAX ~0u
#define SI4_MAX ~0 >> 1
#define SI4_MIN ~0
#define zxINT_SIZE 4
#define zxUCHAR_MAX ~0u >> 24 

As you may have noticed this is part of a larger set of tests.
Last edited on
Removing the assumption about sizeof(int):

1
2
3
4
5
6
enum { iUCHAR_MIN = (unsigned char)0u, iUCHAR_MAX = (unsigned char)~0U,
       iUCHAR_BITS = ( iUCHAR_MAX == 0x7f ? 7 : ( iUCHAR_MAX == 0xff ? 8 : 9 ) ) } ;

#define zxUCHAR_MIN ((unsigned char)iUCHAR_MIN)
#define zxUCHAR_MAX ((unsigned char)iUCHAR_MAX)
#define zxUCHAR_BIT ((unsigned int)iUCHAR_BITS) 
Thanks, I'll give it a try, failing that I'll add it to a new file I'm making that detects architectures.

Edit: This seems to never reach the #error directive I added so I will use what you have suggested, Thank you :) BTW I kept the CHAR_BIT outside the enum and in an #if directive
Last edited on
unsigned up to short works fine
signed up to long/int works fine
need help for larger values (checked via preprocessor)

Edit: Never mind, seems my Intellisense is having issues, compiler has no such issue though static T const NAME = ~0U; is the format I used
Last edited on
Topic archived. No new replies allowed.