B
Brian
Shao said:Brian said:Keith said:Eric Sosman wrote:
[...]
- What is the type of `1'?
I would assume it would be "coerced" to the type of n, yes? [...]
The type of 1 is int.
No matter what the type of n is?
You might enjoy a draft with filename 'n1256.pdf'. Check out section
6.5.7. The '1' is promoted regardless of 'n'. Since plain '1' is an
'int', the result of the operator and its operands will be an 'int'.
Then 'n' is used to shift the promoted '1'. If 'n' is >= the width of
an 'int', the behaviour is undefined. The width of an integer type
includes the sign bit, whether or not there is one.
If we have 'int' (a signed integer type, per 6.2.5p4) with 15 value
bits and one sign bit, '1' might look like:
Pos: SEDCBA9876543210
Bit: 0000000000000001
Then '<<' is defined up to an 'n' (in your macro) of 15 (width - 1):
Pos: SEDCBA9876543210
Bit: 1000000000000000
This so happens to be the representation of a negative zero in a
sign-and-magnitude binary signed representation. It could actually
be a trap representation, causing trouble. So unless you can
guarantee that it's not a trap representation, you're only safe up to
an 'n' of 14 (under these circumstances).
Regardless of that, if you shift any further, the result does not get
any extra bits added to it. That means that if you use it in a
bitwise operation with some other operand larger than an 'int', you
mightn't get what you expect. The program might even crash. Assuming
it doesn't, you could still have something like:
Pos: SEDCBA9876543210FEDCBA9876543210
Bit: ???????????????????????????????? (1 << 16)[see note]
Bit: 00000000000000010000000000000000 & (65536)
Bit: 000000000000000?0000000000000000 (result)
Hope that helps. Perhaps I've gotten it wrong.
[note] Undefined, then usual arithmetic conversions[6.3.1.8] applied
for '&' operator evaluation.
Yeah, ok, thx. But really, this is hardly the time for me to analyze the
lowest levels of computer architecture or C. I was just cleaning up the
bitset stuff because I was working on the file API which led me there. I
AM trying to get my codebase to a place where I will be able to bring in
contractors to do the stuff I don't care to do (or that I won't live long
enough to figure out, it's all about time). Of course they will have to
be able to adapt to house standards and leave ISO at the door (along with
signing non-disclosure agreement and committing to "work done for hire").
Who knows, maybe even one of you guys (not likely, but weird things do
happen sometimes) will do the final testing (whitebox even, but not on
anything that reveals the product or constituents at a high level) of the
software. Right now, as long as my file and GUI libs work, all is good,
as there is a lot of important work still to be done
I'm glad I posted the macros. The dialog that ensued brought me out of
"code mode" and back into people-issue land. Finally, I will say that my
project is not yet funded, other than my own blood, sweat and tears. (One
of my products, but not the initital, may indeed be embedded if licensing
and similar issues can be resolved).