negative hex

So, ten is the only radix in which negative numbers can be expressed? Then it's sure a piece of luck that we evolved ten fingers, otherwise we'd be stuck working with only positive numbers.

John

Reply to
John Larkin
Loading thread data ...

Well, do you know of any species that uses negative numbers that doesn't have ten fingers?

What do they teach in school nowadays?

Cheers,

Phil Hobbs

Reply to
Phil Hobbs

oh. Good point.

John

(slinks away, humiliated)

Reply to
John Larkin

Dolphins and chimps can do some math:

No clue if they can do negative numbers or hexadecimal.

Politics, social adaptation, complacency, conformity, consumerism, political correctness, gang organization, violence, fighting, intimidation, controlled substance abuse, pharmacology, and various forms of criminal activity.

--
Jeff Liebermann     jeffl@cruzio.com
150 Felker St #D    http://www.LearnByDestroying.com
Santa Cruz CA 95060 http://802.11junk.com
Skype: JeffLiebermann     AE6KS    831-336-2558
Reply to
Jeff Liebermann

Vile slanders. My kid majored in softball and beer pong.

John

Reply to
John Larkin

Well, it's your program, so you can write it any way you please.

Me? Well, I figure that I'll have to go back to my code in a year or three to do some maintenance and enhancement. So I write it as clearly and cleanly as possible, following convention.

If I do something unusual, I comment it carefully so I'll know what I did and why I did it.

But feel free to write any way you please. I won't the be guy stuck maintaining your code.

Somewhere along the line, though, you'll probably figure out that it's best to follow convention and common sense.

Tom

Reply to
Tom2000

John, I've been sitting on the side wondering whether to put in 2c worth, and this seems like it is the time and place.

Surely it can't be "negative" if it is a pattern of bits. The bits are 0 or 1. Not a negative option in sight.

Reply to
rebel

Actually, rather than letting my previous answer stand, I realize after re-reading your original message that you might just be confused about this stuff. So I'll try an explanation.

Your original example showed you negating a hex number that had the MSB set, something like 0xfecd.

When the compiler sees a number expressed in hex or binary, and that number is either assigned to a signed integer variable or used as a constant, the compiler will treat that as a two's complement signed number. If the MSB is set, that indicates that it's a negative number. If the MSB is clear, it's positive.

When you negated that number, you were applying a double negative. If you had written -0xfecd, for instance, the compiler would simply interpret that number as 0x0133. In decimal, that would be the same as writing -(-307).

There's a long chance that's what you were intending to write, but somehow I doubt it. My guess is that when you compile and run your program you won't get the result you expect.

If you did, indeed, mean to negate that number, expressing it in decimal, where the negation sign has meaning, would the proper way to handle the expression. Or, if you want to stay in hex, writing something like 'val = 0xfecd * -1' would do the trick and still be readable. Your intent would be clear to the person who had to maintain your code.

Hanging a neg sign in front of a hex number is confusing, and likely to be an error generator. If someone dropped your program on my desk and said "fix this," the first thing I'd do after seeing a negated hex number would be to chase you down with a baseball bat. :-)

HTH,

Tom

Reply to
Tom2000

Why? Why should the decimal system be any different than an octal, binary or hexadecimal system?

--
Programmeren in Almere?
E-mail naar nico@nctdevpuntnl (punt=.)
Reply to
Nico Coesel

The fun part is doing real numbers in other bases. Like base pi.

Tim

--
Deep Friar: a very philosophical monk.
Website: http://webpages.charter.net/dawill/tmoranwms
Reply to
Tim Williams

Un bel giorno John Larkin digitò:

0x is C syntax, therefore the convention exists and it is the C language. The second form is just wrong; the first form is parsed correctly, but in C the hex constants (as well as the octal constants) are treated as unsigned. Therefore -0xFFFF is a 17-bit number (-65535) and in 16-bit architectures the compiler will likely issue a warning and then treat the costant as a signed number. For example, if I try this with Visual C++ on a 32 bit platform:

long long test = -0xFFFFFFFF;

'test' will be assigned with the value 1.

--
emboliaschizoide.splinder.com
Reply to
dalai lamah

On a sunny day (Sun, 29 Jun 2008 15:06:24 GMT) it happened dalai lamah wrote in :

C knows the '%i' numeric input conversion, from libc.info: The %i' conversion matches an optionally signed integer in any of the formats that the C language defines for specifying an integer constant. The syntax that is recognized is the same as that for the trtol' function (*note Parsing of Integers::) with the value for the BASE argument. (You can print integers in this syntax with rintf' by using the #' flag character with the %x', %o', or %d' conversion. *Note Integer Conversions::.)

The folowing C program 'test_.c' shows some of its features: int main(int argc, char **argv) { int first, second, result;

fprintf(stderr, "Enter first number\\n"); fscanf(stdin, "%i", &first);

fprintf(stderr, "Enter second number\\n"); fscanf(stdin, "%i", &second);

result = first - second;

fprintf(stderr, "The first number is %d, the second number=%d, and the difference=%d\\n", first, second, result);

exit(1); } /* end function main */

Compile it: gcc -o test_ test_.c

Run it: grml: ~ # ./test_ Enter first number

3 Enter second number 1 The first number is 3, the second number=1, and the difference=2

grml: ~ # ./test_ Enter first number

0xffff Enter second number 0x00ff The first number is 65535, the second number=255, and the difference=65280

grml: ~ # ./test_ Enter first number

0xffffff Enter second number 0xabcd The first number is 16777215, the second number=43981, and the difference=16733234

grml: ~ # ./test_ Enter first number

0xff Enter second number 0x1ffffffff The first number is 255, the second number=2147483647, and the difference=-2147483392

We should note that John is an avid asm programmer, but life could perhaps be easier in cases like this with C. The risk of making errors in a 6000 line parser should be compared to the risk of errors in a C library and a few lines of C code..

Reply to
Jan Panteltje

I'm asking about how one would *express* a number which results from the negation of an integer which is itself expressed in hex. A true integer is a point on the infinite number line. In decimal, "256" names one of those points in decimal; if we agree on the syntax, 0x100 names that same point in rad16.

Decimal -256 names a different point, one on the opposite side of the origin from 256. So how should we agree to name that point in hex? I think we agree that -0x100 is good.

Once we start to cram pure-mathematical integers into finite-length binary registers, it gets a bit more complicated. It's still simple as long as we agree on a naming convention for signed values (and 2's complement is almost always the choice) and we don't overflow the number of available bits. In 2's complement, if the most significant bit of the register is set, we call the number negative.

8-bit wide, 2's complement convention:

0 0 0 0 0 0 0 0 = decimal 0

0 0 0 0 0 0 0 1 = decimal 1 0 0 0 0 0 0 1 1 = decimal 3 0 1 1 1 1 1 1 1 = decimal 127 1 0 0 0 0 0 0 0 = decimal -128 1 1 1 1 1 1 1 1 = decimal -1

John

Reply to
John Larkin

OK, what's the convention for expressing negative numbers, as parsable ascii strings, in base 16? That was, after all, my original question.

John

Reply to
John Larkin

You could store polarity in one word, and the value in another, and then make a determination based on the data in the word pair.

Reply to
Archimedes' Lever

, and

e 0 or 1.

I would like to say first that John (Larkin) has a zero-defect understanding of his numbering systems, to all those who have offered there thoughts. He is not confused in the least. :) The answer his question is not so much and answer about numbering system, but more of a "Where is the restroom?" question.

The answer is that, traditionally, no syntax has been prescribed for expressing negative constants when the base is 16. This has been true for most, if not all, of the popular modern languages including C/C++.

As he noted, there is nothing that would have prevented the language designer from taking the exact same approach to base 16 as any other base. One could even arge that a regular language might have allowed any base between two reasonable values, say 2 and 36, with appropriate encoding and an explicit negative or positive sign affixed at left.

-Le Chaud Lapin-

Reply to
Le Chaud Lapin

AFAIK, the convention is not to do it. But if you must, I don't seen anthing wrong with something like -0x100. After all, we commonly use the unary 1's complement '~' simiarly as in ~0x100. The '-' symbol is somewhat more troublesome to parse because it's used as both a binary operator and a unary operator, but you know that.

That said, I can't think of a single case where I'd really want to use (or see) signed hex numbers.

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

I don't feel a bit confused.

Oh, thank you so much.

I originally said nothing about the word length, or indeed that I was dealing with computers, so you can't know where the MSB is. The mathematical set of integers has no MSB.

Always?

I'm writing a command parser, in assembly, so I can do whatever I want. Since my parser delivers integers as 2's comp, 32-bits, an input string like "-0xfecd" means -65229 decimal to me. I was just wondering if there was a convention.

It's not my code that's the issue. A user can send me an ascii string of mixed text tokens and numeric args, and I want to allow him/her to use decimal or hex, signed or not, for the numerics. It's my job to document the rules for the user and parse it appropriately.

Hah, you'll have to catch me first.

Oh, I tried some cases on my assembler, the C32 meta-assembler.

000000 1234 .WORD 0x1234 000002 1234 .WORD 1234h 000004 EDCC .WORD -0x1234 000006 EDCC .WORD -1234h 000008 FEEE .WORD 0xFEEE 00000A FEEE .WORD 0FEEEh
Reply to
John Larkin

In C and C++ there is a syntax for writing negative hex numbers, e.g.

-0x12fe for expressing (0x12fe * (-1)). But if we want to split hairs, you are right, because the ISO9899:1999 C standard doesn't define a syntax for negative hex constants in chapter 6.4.4.1, only positive (decimal, octal and hex). The reason why you can write -0x12fe is chapter 6.5.3.3 (3), which defines the unary operator "-".

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply to
Frank Buss

Reply to
Frank Buss

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.