473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Negative number system as compile-time constant?


(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
Jun 27 '06 #1
3 2848
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
Jun 28 '06 #2
Christoph Scholtes posted:
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"?

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
Jun 28 '06 #3
Frederick Gotham wrote:

Christoph Scholtes posted:
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"?


OP had all those macros in the original code
and wanted to see how to make them work.

--
pete
Jun 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
16
by: JKop | last post by:
Take a class like the following: class Finger { public: double length; }
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
11
by: drtimhill | last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled. For example, to get the last character in a string, I can enter "x". To get the...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
1
by: Ryan Liu | last post by:
Hi, When my code run to DataRow.Delete() It throws: System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: length at System.Array.Copy(Array sourceArray, Int32...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
1
by: santoshsri | last post by:
Hi All, My C# web application calls a webservice to process a report. It sends XMLs as parameter and in response gets an XML node which stores Binay datatype bin.base64. It makes an instance of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.