Home > Archive > C > June 2006 > Negative number system as compile-time constant?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Negative number system as compile-time constant?
|
|
| Frederick Gotham 2006-06-27, 6:56 pm |
|
(I'm not sure if there's already something in the Standard Library for
doing this... ?)
Is the following macro okay for getting a compile-time constant that
indicates which negative number system the machine uses?
#define NUM_SYS ( -1 & 1 ? -1 & 2 ? TWOS : SIGNMAG : ONES )
How it works:
(1) Firstly, it gets -1, which will either be:
1000 0001 Sign-mag
1111 1110 One's complement
1111 1111 Two's complement
(2) It AND's it with 1. If the result is false, then we've got one's
complement (as you can see from the bit-pattern above).
(3) It then AND's it with 2. If the result is true, then we have two's
complement. Otherwise, it's sign-magnitude.
Possibly used something like as follows:
#define SIGNMAG "Sign-Magnitude"
#define ONES "One's complement"
#define TWOS "Two's complement"
#define NUM_SYS ( -1 & 1 ? -1 & 2 ? TWOS : SIGNMAG : ONES )
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf( "This machine uses: %s\n", NUM_SYS );
system("PAUSE");
}
Another thing, I naively tried to do the following in my original code:
puts( "This machine uses: " NUM_SYS );
thinking that NUM_SYS would become a string literal which would sit right
beside the one to its left (yielding one string literal, i.e:)
puts( "This machine uses: " "Two's complement" );
I wonder if there's any way of getting that to work... ? Hmm...
--
Frederick Gotham
| |
| Christoph Scholtes 2006-06-27, 9:56 pm |
| Hi Pete,
pete wrote:
> #if -1 & 3 == 1
> #define NUM_SYS SIGNMAG
> #elif -1 & 3 == 2
> #define NUM_SYS ONES
> #else
> #define NUM_SYS TWOS
> #endif
>
> #define SIGNMAG "Sign-Magnitude"
> #define ONES "One's complement"
> #define TWOS "Two's complement"
Is there a specific reason to use the second block of defines instead of
putting them in the according lines above, like #define NUM_SYS
"Sign-Magnitude"?
Chris
| |
| Frederick Gotham 2006-06-27, 9:56 pm |
| Christoph Scholtes posted:
> Hi Pete,
>
> pete wrote:
>
>
> Is there a specific reason to use the second block of defines instead
of
> putting them in the according lines above, like #define NUM_SYS
> "Sign-Magnitude"?
Style. Same reason why someone writes:
const char *p;
instead of:
char const* p;
Another thing is that we can re-use the macros, e.g.:
puts( "My system is " NUM_SYS ", but I'd rather it were " TWOS "." );
--
Frederick Gotham
| |
|
| Frederick Gotham wrote:[color=darkred]
>
> Christoph Scholtes posted:
>
OP had all those macros in the original code
and wanted to see how to make them work.
--
pete
|
|
|
|
|