IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New What is with gcc, anyway?!?
OK folks...Here's today's quiz:

Given the following C code:
\n#define MAX(a, b)    ((a) > (b) ? (a) : (b))\n\nint main (int argc, char ** argv)\n{\n    assert(MAX(0x7FFFFFFF, 0x80000000) == 0x7FFFFFFF);\n    return 0;\n}\n


When compiled for a 32-bit architecture (x86), why would this assert?

When compiled with gcc (V3.3.1), it does.

According to the ANSI standard:

The type of an integer constant is the first of the corresponding list in which its value can
be represented.










suffixDecimal
Constant
Octal or Hex
Constant
Noneint
long int
long long int
int
unsigned int
long int
unsigned long int
long long int
unsigned long long int


Well, we have no suffix, and both values can be represented as ints, so they should both be ints, and so therefore 0x7FFFFFFF is greater than 0x80000000, so the assertion should be true.

So what's the deal? Is it that the gcc people can't be bothered with ANSI compliance? Or is there some kind of error here that I'm just not seeing? (To be fair, this was gotten via cygwin; perhpas the cygwin people dicked it up?)

(And don't even get me started about the fact that they can't seem to be bothered implementing __STDC_VERSION__....)

/me is not pleased!
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New What is typeof(0x80000000)?
[edit note: I guess I did read it backwards after all - NeverMind].

I'm assuming that you think the number should be negative? By your table,

  1. int
  2. unsigned int
  3. long int
  4. unsigned long int
  5. long long int
  6. unsigned long long int

I don't see how you expect to ever have a constant of type of unsigned, since the number of bits involved is always the same as the signed counterpart? That is, how could you ever get an unsigned integer between 0 & 255, when the signed integer would go from -128 to 127 over those same 8 bits?

Perhaps I'm missing something obvious though.
Expand Edited by ChrisR Aug. 26, 2004, 06:25:16 PM EDT
New right, add an L
-drl
New Ain't that a UL
My See is awful rusty.
New This is why C is sometimes called "high-level assembler."
Although I enjoyed using C to poke around inside MS-DOS and Windows, nowadays I prefer more modern languages. With things like Big Integers. :-)

Wade.

Is it enough to love
Is it enough to breathe
Somebody rip my heart out
And leave me here to bleed
 
Is it enough to die
Somebody save my life
I'd rather be Anything but Ordinary
Please

-- "Anything but Ordinary" by Avril Lavigne.

New Re: This is why C is sometimes called "high-level assembler.
C is basically the lingua franca for embedded these days (although C++ is starting to make inroads there, much to Todd's chagrin ;-) )
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Actually, that would be a U (or u)
The 'L' or 'l' means long and implicitly signed. Note, however, that
\nint main (int argc, char ** argv)\n{\n    assert(MAX(0x7FFFFFFFL, 0x80000000L) == 0x7FFFFFFFL);\n    return 0;\n}\n\n

also asserts. And while LordBeatnik's respone below makes sense and probably explains my original bitch, it certainly doesn't explain the above.

<mumble>
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New What about casting the values?
something like:
\n   assert(MAX((long)0x7FFFFFFF, (long)0x80000000) == (long)0x7FFFFFFF);\n
New Yes, that was the solution I used
This seems to be the best solution; it gives me the explicit bit pattern I'm looking for, and the explicit instruction to treat the patters as signed. Hopefully, when the first draft of the C++0x spec comes out, they will have cleaned up the spec with regards to this particular issue.
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Re: What is with gcc, anyway?!?
It looks reasonable to me that 0x8000000 got promoted to unsigned int. If it wasn't, what would be the point of having the unsigned types in the list? That's why the unsigned types are not listed for decimal numbers but are for hex. 0x80000000 wouldn't be positive as an int, so it got promoted to unsigned int. Thus, the MAX returns 0x80000000.

Dave "LordBeatnik"
New I guess...
What you say makes sense. The problem here is the phrase in the standard
The type of an integer constant is the first of the corresponding list in which its value can be represented. [emphasis added]
0x8000000 certainly "can be represented" as an int, so silly /me thought that, if it "can be represented" as an int, it would be.

Thanx for the input!
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New If there was no ambiguity...
If there were no ambiguities in standards, what reason would they have to host another party^Wstandards meeting? 8)

Dave "LordBeatnik"
New For the next set of standards?
/me slaps head in disbelief that I even thought that...
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Hex constants are assumed to be positive
0x80000000 would make an int to be negative, so it cannot be represented as int.
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New Re: Hex constants are assumed to be positive
Sez who?

(OK, let me rephrase that with the smart-ass control turned down...Where in the ANSI standard does it say that hex constants are assumed to be positive?)
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Setting the high bit is processor dependent.
It's not carved in stone anywhere that that's the only way to do it. Most do, but that's not specified generally anywhere.
New OK, then the standard should be saying
"presentable in the implementation's architecture". What do you do with those nits, eat them?
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New No I actually write code based on them
and then spend entirely too much time recovering from other's inability to use the English language effectively. What do you do with them?
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Ignore them and use my common sence
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New I'll bet debugging your code is a real joy...
;-)
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Nope
The joy comes when you read your standard, forget your common sence and assume that same things work the same way everywhere.
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New And so tell me, O Oracle of Common Sence [sic]
What exactly is it that is so nonsensical about defining a hex value and assuming it is treated as a signed value? We supplicate ourselves breathlessly at your feet awaiting such pearls (or is that perls) of wisdom as you may deign to proffer.
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New offensive foul, ball to Ark
You violated the "be kind to non-native English speakers" (who do extremely well in a totally different language and make us look stupid by comparison) rule.

Just point out that it's "sense".

BTW Ark - "licence" and "license" are both right. One is British. Likewise "offence" and "offense", "defence" and "defense". But "electric fense" is always wrong.
-drl
New Rest assured,
I'd be making the same mistakes in Russian as well.

Por syntax checus in Cium - Deo Gratie.
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New :) ok
-drl
New OK, OK...
I'll take my yellow card and give up an indirect free kick....




;-)
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

Expand Edited by jb4 Aug. 31, 2004, 11:26:59 AM EDT
New Well, while the tone is disagreable, the question is
(almost) legit

\n#include <stdio.h>\n\n\nint main(int argc, char**argv) {\n    int c = -0xFF;\n    printf("signed: %d, hex: %X\\n", c, c);\n    return 0;\n}\n


\n$ gcc -o test test.c; ./test\nsigned: -255, hex: FFFFFF01\n


And the answer is <drrrrum rrrroll>: The hex constants are treated as positive because they lack the "minus" in front. Tada!



--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New Point missed.
The point is not that I can generate a negative number using hex values, its that I can generate a specific bit pattern directly that is interpreted correctly using two's compliment arithmetic. In my example, I want the bit pattern 0x80000000. Interpreted as an int. Which is what I should expect, given that incomplete pile of verbage that passes as the C(99) standard in section 6.4.4.1, paragraph 5. Your example doesn't match, as you explicitly change the value of your bit pattern by applying an operator to it (the unary minus).
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New If C were sane, TRUE = -1 and problem vanishes
-drl
New Ermm...say What?
You got me on that one, Ross. How does the value of a boolean have anything to do with how a constant token is interpreted?
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Re: Ermm...say What?
It forces strict attention to signed vs. unsigned, because TRUE is signed.

IOW we don't need no steenkin Booleans.
-drl
New Much like Jewish law
Forces strict attention to matters divine because it's so easy to transgress. Any takers?

You recommend a practice because it's easy to violate? Ouch.
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New So you prefer how VB does it??
To deny the indirect purchaser, who in this case is the ultimate purchaser, the right to seek relief from unlawful conduct, would essentially remove the word consumer from the Consumer Protection Act
- [link|http://www.techworld.com/opsys/news/index.cfm?NewsID=1246&Page=1&pagePos=20|Nebraska Supreme Court]
New Point: Ben...
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New How it used to do it.
VB.Net had to play a little nicer with the other languages, so they switched to the way it's represented in C#.
New Hehee!
VB was "embraced and extended" by C! Oh, the irony!
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Why do you expect hex to be treated differently from decimal
?

When you see a decimal constant 4294967294, do you expect it to mean -2?
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
Expand Edited by Arkadiy Aug. 31, 2004, 11:32:44 AM EDT
New Re: Why do you expect hex to be treated differently
In FORTH this issue never arises. An integer is what it is in relation to base 2 and the number of available bits in it. Signed vs. unsigned is a stupid, needless complication.

So: TRUE is 111111...1 and FALSE is 000000...0. There is only one TRUE and one FALSE and NOT TRUE is FALSE. Something like 1101001..1 has no logical value.

The programmatic representation is immaterial.
-drl
New Man, that's a good question!
I suppose because I can specify a bit pattern directly, and I expected that bit pattern to be interpreted by the underlying hardware directly. But I must say, I now see your point, and it is a good one.

Thanx!
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Now that's clarity!
Alex

"If you can control the meaning of words, you can control the people who must use the words." -- Philip K. Dick, US science fiction writer
New Nope, it's bad math
A 32 bit integer has 4...mumble possible values. If you really want integers and not whole numbers, then implicitly these are signed, which requires a bit. So there are 2...mumble positive values, 2...mumble-1 negative values, and zero.

If you want 4...mumble integers you need more bits.
-drl
New But, counters of real things don't need negative values.
Alex

"If you can control the meaning of words, you can control the people who must use the words." -- Philip K. Dick, US science fiction writer
New I'd like to add a negative number of votes for Bush. ;-)
To deny the indirect purchaser, who in this case is the ultimate purchaser, the right to seek relief from unlawful conduct, would essentially remove the word consumer from the Consumer Protection Act
- [link|http://www.techworld.com/opsys/news/index.cfm?NewsID=1246&Page=1&pagePos=20|Nebraska Supreme Court]
New That only works on Diebold machines. :)
Alex

"If you can control the meaning of words, you can control the people who must use the words." -- Philip K. Dick, US science fiction writer
New Certainly
But then, Ark's comment loses its pithiness (\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd)
-drl
New "Expressive", not "pithy" :)
"pithy" I guess would be better translated as poisonous, ядовитый - it's an idiomatic usage.
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New Re: "Expressive", not "pithy" :)
Why can I see your Cyrillic but not mine? Some encoding mojo?
-drl
New Dunno.
I see yours fine in Mozilla on Win 2000 after I select the proper code page - Cyrillic Windows-1251
--

"...was poorly, lugubrious and intoxicated."

-- Patrick O'Brian, "Master and Commander"
New Wasn't trying to piss you off...
From my point of view, hex and octal are just more convienient ways to write binary.
Hex/octal/binary numbers are not negative without some convention to make them so. This varies.
The C language, and those that borrowed from it, make the tacit assumption that the high bit indicates a negative number unless it is declared unsigned, in which case it is just a binary number in the range of 0 - UINT_MAX (from limits.h) I don't know of any languages where you can write -0x123 for a negative hex number.
I should mention that I am not particularly a language pedant. I currently use C/C++ for some application work and drivers, I write firmware for a propriatary processor in a much modified, self maintained assembler, and do scripting in perl as necessary. Plus whatever needs be done. I'm not a purist. I really wasn't trying to pick nits.
New Interesting assumption:
The C language, and those that borrowed from it, make the tacit assumption that the high bit indicates a negative number unless it is declared unsigned...

An interesting assumption. I don't believe C makes that assumption at all. The underlying hardware makes that assumption, because that's simply the way it works. C (the language) doesn't care. An implementation of C cares, because it has to map tokens that represent negative values to the actual binary representation of the value.

Now it has been several Decades since I saw a machine that doesn't uset two's complement arithmetic. The first one was an NCR 315 that our local bank used when I was in high school in 1968...It was a DECIMAL machine that used XS-3 notation internally. the last one I saw was a CDC Star, which is a 60-bit (!) machine that used ones complement artihmetic...which has a unique value for negative zero. The NCR machine antedated C by something like a decade, but were a C compiler to exist for that machine, it could not assume the high-order bit of a value made it negative.
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Hmm...while the datatype will be first reached,
IIRC, C standard does not state the size of the number, merely that shorts will be a minimum of a certain size, longs will be a minimum of a certain size, etc.

So you're short may handle sizes past 32,768.
New True, but...
...that's not the issue here. The table indicates the progression starts at int, through long, then through long long. So we know that the value is "at least" an int. Since the target machine is a 32-bit machine, we know that the value is treated in some form of 32-bit value.
jb4
shrub\ufffdbish (Am., from shrub + rubbish, after the derisive name for America's 43 president; 2003) n. 1. a form of nonsensical political doubletalk wherein the speaker attempts to defend the indefensible by lying, obfuscation, or otherwise misstating the facts; GIBBERISH. 2. any of a collection of utterances from America's putative 43rd president. cf. BULLSHIT

New Re: What is with gcc, anyway?!?
Depends on whether the compiler implicitly promotes them to signed or unsigned integers.

The answer could change depending on whether your compiler treats 0x7fffffff and 0x80000000 as signed or unsigned integers.

I think most compilers would treat them as signed, but gcc might not. Also, in a 64 bit compiler, both would still be positive integers.

There was a guy I worked with at BEA that had a set of about 100 macros that would promote the values and test, and then dump out the compiler assumptions in a report. Very useful when you have to write portable code.

His name is David Tribble. I wonder where he is working now.

Glen
Expand Edited by gdaustin Aug. 31, 2004, 11:10:58 PM EDT
     What is with gcc, anyway?!? - (jb4) - (52)
         What is typeof(0x80000000)? - (ChrisR) - (7)
             right, add an L -NT - (deSitter) - (6)
                 Ain't that a UL - (ChrisR) - (5)
                     This is why C is sometimes called "high-level assembler." - (static) - (1)
                         Re: This is why C is sometimes called "high-level assembler. - (jb4)
                     Actually, that would be a U (or u) - (jb4) - (2)
                         What about casting the values? - (ChrisR) - (1)
                             Yes, that was the solution I used - (jb4)
         Re: What is with gcc, anyway?!? - (lordbeatnik) - (3)
             I guess... - (jb4) - (2)
                 If there was no ambiguity... - (lordbeatnik) - (1)
                     For the next set of standards? - (jb4)
         Hex constants are assumed to be positive - (Arkadiy) - (36)
             Re: Hex constants are assumed to be positive - (jb4) - (35)
                 Setting the high bit is processor dependent. - (hnick) - (34)
                     OK, then the standard should be saying - (Arkadiy) - (33)
                         No I actually write code based on them - (jb4) - (30)
                             Ignore them and use my common sence -NT - (Arkadiy) - (29)
                                 I'll bet debugging your code is a real joy... - (jb4) - (28)
                                     Nope - (Arkadiy) - (27)
                                         And so tell me, O Oracle of Common Sence [sic] - (jb4) - (26)
                                             offensive foul, ball to Ark - (deSitter) - (3)
                                                 Rest assured, - (Arkadiy) - (1)
                                                     :) ok -NT - (deSitter)
                                                 OK, OK... - (jb4)
                                             Well, while the tone is disagreable, the question is - (Arkadiy) - (21)
                                                 Point missed. - (jb4) - (20)
                                                     If C were sane, TRUE = -1 and problem vanishes -NT - (deSitter) - (7)
                                                         Ermm...say What? - (jb4) - (2)
                                                             Re: Ermm...say What? - (deSitter) - (1)
                                                                 Much like Jewish law - (Arkadiy)
                                                         So you prefer how VB does it?? -NT - (ben_tilly) - (3)
                                                             Point: Ben... -NT - (jb4)
                                                             How it used to do it. - (ChrisR) - (1)
                                                                 Hehee! - (jb4)
                                                     Why do you expect hex to be treated differently from decimal - (Arkadiy) - (11)
                                                         Re: Why do you expect hex to be treated differently - (deSitter)
                                                         Man, that's a good question! - (jb4)
                                                         Now that's clarity! -NT - (a6l6e6x) - (8)
                                                             Nope, it's bad math - (deSitter) - (7)
                                                                 But, counters of real things don't need negative values. -NT - (a6l6e6x) - (6)
                                                                     I'd like to add a negative number of votes for Bush. ;-) -NT - (ben_tilly) - (1)
                                                                         That only works on Diebold machines. :) -NT - (a6l6e6x)
                                                                     Certainly - (deSitter) - (3)
                                                                         "Expressive", not "pithy" :) - (Arkadiy) - (2)
                                                                             Re: "Expressive", not "pithy" :) - (deSitter) - (1)
                                                                                 Dunno. - (Arkadiy)
                         Wasn't trying to piss you off... - (hnick) - (1)
                             Interesting assumption: - (jb4)
         Hmm...while the datatype will be first reached, - (Simon_Jester) - (1)
             True, but... - (jb4)
         Re: What is with gcc, anyway?!? - (gdaustin)

But she said things I just couldn't fathom. She was too deep. Seemed to be under a lot of pressure. Boy, could she drink. She drank like a... she drank a lot.
331 ms