What is the meaning of “producing negative zeroes” in a system that doesn't support it?2019 Community...
Are small insurances worth it
Are Wave equations equivalent to Maxwell equations in free space?
Can you run a ground wire from stove directly to ground pole in the ground
Does the in-code argument passing conventions used on PDP-11's have a name?
Who is at the mall?
What is the purpose of a disclaimer like "this is not legal advice"?
Is this nominative case or accusative case?
The (Easy) Road to Code
Searching for a string that contains the file name
Under what conditions would I NOT add my Proficiency Bonus to a Spell Attack Roll (or Saving Throw DC)?
Quitting employee has privileged access to critical information
School performs periodic password audits. Is my password compromised?
Does it cost more to counter Sphinx of New Prahv?
How spaceships determine each other's mass in space?
Create chunks from an array
Faulty RAID1 disk now shows as foreign
Why would the IRS ask for birth certificates or even audit a small tax return?
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
Where is this quote about overcoming the impossible said in "Interstellar"?
Learning to quickly identify valid fingering for piano?
What is the meaning of option 'by' in TikZ Intersections
Is being socially reclusive okay for a graduate student?
Was it really inappropriate to write a pull request for the company I interviewed with?
Is divide-by-zero a security vulnerability?
What is the meaning of “producing negative zeroes” in a system that doesn't support it?
2019 Community Moderator ElectionWhat does “static” mean in C?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Does the C99 standard guaranteed the binary representation of unsigned int?Can a int16_t to int conversation result in implementation-defined behavior?Is the non-negative range of a signed C++ integer at least as big as the negative range?Are there any implementations that support a negative zero, or reserve it as a trap representation?What does POSIX mean by “two's complement representation” in stdint.h?Can bitwise operators have undefined behavior?When are bitwise operations undefined in C?Is it possible to test whether a type supports negative zero in C++ at compile time?
C17 6.2.6.2/4 says:
If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<,
and >> operators with operands that would produce such a value is undefined.
If I have a 2's complement system, it does not support negative zeroes. And it always utilizes all possible combinations of a binary number to express a value. Therefore it is impossible to ever produce a negative zero, no matter which bitwise operation that is used. So what is the meaning of this text?
My take is that this part refers to systems with 1's complement or signed magnitude that does not support negative zeroes, but instead use a padding bit or trap representation. Is this correct?
c language-lawyer bitwise-operators signed twos-complement
add a comment |
C17 6.2.6.2/4 says:
If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<,
and >> operators with operands that would produce such a value is undefined.
If I have a 2's complement system, it does not support negative zeroes. And it always utilizes all possible combinations of a binary number to express a value. Therefore it is impossible to ever produce a negative zero, no matter which bitwise operation that is used. So what is the meaning of this text?
My take is that this part refers to systems with 1's complement or signed magnitude that does not support negative zeroes, but instead use a padding bit or trap representation. Is this correct?
c language-lawyer bitwise-operators signed twos-complement
What do you understand by "trap representation" ?
– alinsoar
5 hours ago
I also read lots of times about "tr" but I never saw one concretely, so I cannot understand what that means. I read articles about this, but I do not understand what is a trap representation. On X86 processors are there trs ?
– alinsoar
5 hours ago
1
@alinsoar I don't know of any implementation in the real world that uses them for plain integers (floating point is another story). Mostly it is something invented by C standard committee language lawyers, like they invented the need to support 1's complement and signed magnitude computers.
– Lundin
4 hours ago
1
I don't get why there are complicated answers and why this is even a question, doesn't it just mean exactly what it says? You are asking about a system which does not contain the notion of a negative zero, so isn't that exactly what that section would be describing? Meaning, just as your quoted section says... the behavior that would produce a negative zero (that behavior which doesn't exist in your case) has undefined results. As simple as that. Analogy: Enacting a law saying "Anyone who slays a boogeyman gets a week in jail" is not suddenly confusing simply because of a lack of boogeymen.
– Aaron
34 mins ago
add a comment |
C17 6.2.6.2/4 says:
If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<,
and >> operators with operands that would produce such a value is undefined.
If I have a 2's complement system, it does not support negative zeroes. And it always utilizes all possible combinations of a binary number to express a value. Therefore it is impossible to ever produce a negative zero, no matter which bitwise operation that is used. So what is the meaning of this text?
My take is that this part refers to systems with 1's complement or signed magnitude that does not support negative zeroes, but instead use a padding bit or trap representation. Is this correct?
c language-lawyer bitwise-operators signed twos-complement
C17 6.2.6.2/4 says:
If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<,
and >> operators with operands that would produce such a value is undefined.
If I have a 2's complement system, it does not support negative zeroes. And it always utilizes all possible combinations of a binary number to express a value. Therefore it is impossible to ever produce a negative zero, no matter which bitwise operation that is used. So what is the meaning of this text?
My take is that this part refers to systems with 1's complement or signed magnitude that does not support negative zeroes, but instead use a padding bit or trap representation. Is this correct?
c language-lawyer bitwise-operators signed twos-complement
c language-lawyer bitwise-operators signed twos-complement
asked 7 hours ago
LundinLundin
111k17161269
111k17161269
What do you understand by "trap representation" ?
– alinsoar
5 hours ago
I also read lots of times about "tr" but I never saw one concretely, so I cannot understand what that means. I read articles about this, but I do not understand what is a trap representation. On X86 processors are there trs ?
– alinsoar
5 hours ago
1
@alinsoar I don't know of any implementation in the real world that uses them for plain integers (floating point is another story). Mostly it is something invented by C standard committee language lawyers, like they invented the need to support 1's complement and signed magnitude computers.
– Lundin
4 hours ago
1
I don't get why there are complicated answers and why this is even a question, doesn't it just mean exactly what it says? You are asking about a system which does not contain the notion of a negative zero, so isn't that exactly what that section would be describing? Meaning, just as your quoted section says... the behavior that would produce a negative zero (that behavior which doesn't exist in your case) has undefined results. As simple as that. Analogy: Enacting a law saying "Anyone who slays a boogeyman gets a week in jail" is not suddenly confusing simply because of a lack of boogeymen.
– Aaron
34 mins ago
add a comment |
What do you understand by "trap representation" ?
– alinsoar
5 hours ago
I also read lots of times about "tr" but I never saw one concretely, so I cannot understand what that means. I read articles about this, but I do not understand what is a trap representation. On X86 processors are there trs ?
– alinsoar
5 hours ago
1
@alinsoar I don't know of any implementation in the real world that uses them for plain integers (floating point is another story). Mostly it is something invented by C standard committee language lawyers, like they invented the need to support 1's complement and signed magnitude computers.
– Lundin
4 hours ago
1
I don't get why there are complicated answers and why this is even a question, doesn't it just mean exactly what it says? You are asking about a system which does not contain the notion of a negative zero, so isn't that exactly what that section would be describing? Meaning, just as your quoted section says... the behavior that would produce a negative zero (that behavior which doesn't exist in your case) has undefined results. As simple as that. Analogy: Enacting a law saying "Anyone who slays a boogeyman gets a week in jail" is not suddenly confusing simply because of a lack of boogeymen.
– Aaron
34 mins ago
What do you understand by "trap representation" ?
– alinsoar
5 hours ago
What do you understand by "trap representation" ?
– alinsoar
5 hours ago
I also read lots of times about "tr" but I never saw one concretely, so I cannot understand what that means. I read articles about this, but I do not understand what is a trap representation. On X86 processors are there trs ?
– alinsoar
5 hours ago
I also read lots of times about "tr" but I never saw one concretely, so I cannot understand what that means. I read articles about this, but I do not understand what is a trap representation. On X86 processors are there trs ?
– alinsoar
5 hours ago
1
1
@alinsoar I don't know of any implementation in the real world that uses them for plain integers (floating point is another story). Mostly it is something invented by C standard committee language lawyers, like they invented the need to support 1's complement and signed magnitude computers.
– Lundin
4 hours ago
@alinsoar I don't know of any implementation in the real world that uses them for plain integers (floating point is another story). Mostly it is something invented by C standard committee language lawyers, like they invented the need to support 1's complement and signed magnitude computers.
– Lundin
4 hours ago
1
1
I don't get why there are complicated answers and why this is even a question, doesn't it just mean exactly what it says? You are asking about a system which does not contain the notion of a negative zero, so isn't that exactly what that section would be describing? Meaning, just as your quoted section says... the behavior that would produce a negative zero (that behavior which doesn't exist in your case) has undefined results. As simple as that. Analogy: Enacting a law saying "Anyone who slays a boogeyman gets a week in jail" is not suddenly confusing simply because of a lack of boogeymen.
– Aaron
34 mins ago
I don't get why there are complicated answers and why this is even a question, doesn't it just mean exactly what it says? You are asking about a system which does not contain the notion of a negative zero, so isn't that exactly what that section would be describing? Meaning, just as your quoted section says... the behavior that would produce a negative zero (that behavior which doesn't exist in your case) has undefined results. As simple as that. Analogy: Enacting a law saying "Anyone who slays a boogeyman gets a week in jail" is not suddenly confusing simply because of a lack of boogeymen.
– Aaron
34 mins ago
add a comment |
2 Answers
2
active
oldest
votes
Yes, I think your interpretation is correct. In two's complement, that are no operations that could generate a negative zero, because the concept here doesn't exist: any value that has the sign bit set is necessarily less than 0
.
BTW: It is very likely that the exotic sign representations will be removed from C2x, so all of this will disappear.
1
I'm amazed at why language designers seem to be so worried about backwards hardware compatibility. I don't think any computer hardware commercialised during the current century uses anything but twos complement representation, and one can be rather certain that nobody is going to write a compiler for a new language version designed to work on old hardware (the opposite, running an old language version on new hardware, of course happens a lot). So language designers could have assumed twos complement behaviour for a long time, but apparently they are only coming around to doing that just now.
– Marc van Leeuwen
5 hours ago
@Jens, is the committee then considering specifying two's complement representation as the only option? For another way the exotic sign representations might be removed would be to delete the details, but simply make the representations of signed integers implementation defined (or even unspecified) when the sign bit is set.
– John Bollinger
4 hours ago
@MarcvanLeeuwen, the problem was that it is difficult to assess if there are still such implementations around. And, as always, if people would more engage in committee work, things might go smoother.
– Jens Gustedt
2 hours ago
@JohnBollinger yes, it should be two's complement, see open-std.org/jtc1/sc22/wg14/www/docs/n2330.pdf
– Jens Gustedt
2 hours ago
add a comment |
Your interpretation is correct.
Going up to paragraph 2 of 6.2.6.2:
For signed integer types, the bits of the object
representation shall be divided into three groups: value bits,
padding bits, and the sign bit. There need not be any
padding bits; signed char shall not have any padding bits.
There shall be exactly one sign bit. Each bit that is a
value bit shall have the same value as the same bit in the
object representation of the corresponding unsigned type (if there are
M value bits in the signed type and N in the unsigned type, then M ≤ N
). If the sign bit is zero, it shall not affect the resulting
value. If the sign bit is one, the value shall be modified
in one of the following ways:
- the corresponding value with sign bit 0 is negated ( sign and magnitude );
- the sign bit has the value − (2M)( two’s complement );
- the sign bit has the value − (2M − 1) ( ones’ complement ).
Which of these applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first
two), or with sign bit and all value bits 1 (for ones’
complement), is a trap representation or a normal value. In
the case of sign and magnitude and ones’ complement, if this
representation is a normal value it is called a negative zero.
This means an implementation using either one's complement or sign and magnitude has, for a given size integer type, a specific representation which must be either negative zero or a trap representation. It's then up to the implementation to choose which one of those applies.
As an example, suppose a system has sign and magnitude representation and a 32 bit int
with no padding. Then the representation that would be negative zero, if it is supported, is 0x80000000
.
Now suppose the following operations are performed:
int x = 0x7fffffff;
x = ~x;
If the implementation supports negative zero, the ~
operator will generate -0
as the result and store it in x
. If it does not, it creates a trap representation and invokes undefined behavior as per paragraph 4.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044324%2fwhat-is-the-meaning-of-producing-negative-zeroes-in-a-system-that-doesnt-supp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, I think your interpretation is correct. In two's complement, that are no operations that could generate a negative zero, because the concept here doesn't exist: any value that has the sign bit set is necessarily less than 0
.
BTW: It is very likely that the exotic sign representations will be removed from C2x, so all of this will disappear.
1
I'm amazed at why language designers seem to be so worried about backwards hardware compatibility. I don't think any computer hardware commercialised during the current century uses anything but twos complement representation, and one can be rather certain that nobody is going to write a compiler for a new language version designed to work on old hardware (the opposite, running an old language version on new hardware, of course happens a lot). So language designers could have assumed twos complement behaviour for a long time, but apparently they are only coming around to doing that just now.
– Marc van Leeuwen
5 hours ago
@Jens, is the committee then considering specifying two's complement representation as the only option? For another way the exotic sign representations might be removed would be to delete the details, but simply make the representations of signed integers implementation defined (or even unspecified) when the sign bit is set.
– John Bollinger
4 hours ago
@MarcvanLeeuwen, the problem was that it is difficult to assess if there are still such implementations around. And, as always, if people would more engage in committee work, things might go smoother.
– Jens Gustedt
2 hours ago
@JohnBollinger yes, it should be two's complement, see open-std.org/jtc1/sc22/wg14/www/docs/n2330.pdf
– Jens Gustedt
2 hours ago
add a comment |
Yes, I think your interpretation is correct. In two's complement, that are no operations that could generate a negative zero, because the concept here doesn't exist: any value that has the sign bit set is necessarily less than 0
.
BTW: It is very likely that the exotic sign representations will be removed from C2x, so all of this will disappear.
1
I'm amazed at why language designers seem to be so worried about backwards hardware compatibility. I don't think any computer hardware commercialised during the current century uses anything but twos complement representation, and one can be rather certain that nobody is going to write a compiler for a new language version designed to work on old hardware (the opposite, running an old language version on new hardware, of course happens a lot). So language designers could have assumed twos complement behaviour for a long time, but apparently they are only coming around to doing that just now.
– Marc van Leeuwen
5 hours ago
@Jens, is the committee then considering specifying two's complement representation as the only option? For another way the exotic sign representations might be removed would be to delete the details, but simply make the representations of signed integers implementation defined (or even unspecified) when the sign bit is set.
– John Bollinger
4 hours ago
@MarcvanLeeuwen, the problem was that it is difficult to assess if there are still such implementations around. And, as always, if people would more engage in committee work, things might go smoother.
– Jens Gustedt
2 hours ago
@JohnBollinger yes, it should be two's complement, see open-std.org/jtc1/sc22/wg14/www/docs/n2330.pdf
– Jens Gustedt
2 hours ago
add a comment |
Yes, I think your interpretation is correct. In two's complement, that are no operations that could generate a negative zero, because the concept here doesn't exist: any value that has the sign bit set is necessarily less than 0
.
BTW: It is very likely that the exotic sign representations will be removed from C2x, so all of this will disappear.
Yes, I think your interpretation is correct. In two's complement, that are no operations that could generate a negative zero, because the concept here doesn't exist: any value that has the sign bit set is necessarily less than 0
.
BTW: It is very likely that the exotic sign representations will be removed from C2x, so all of this will disappear.
answered 7 hours ago
Jens GustedtJens Gustedt
65.7k274142
65.7k274142
1
I'm amazed at why language designers seem to be so worried about backwards hardware compatibility. I don't think any computer hardware commercialised during the current century uses anything but twos complement representation, and one can be rather certain that nobody is going to write a compiler for a new language version designed to work on old hardware (the opposite, running an old language version on new hardware, of course happens a lot). So language designers could have assumed twos complement behaviour for a long time, but apparently they are only coming around to doing that just now.
– Marc van Leeuwen
5 hours ago
@Jens, is the committee then considering specifying two's complement representation as the only option? For another way the exotic sign representations might be removed would be to delete the details, but simply make the representations of signed integers implementation defined (or even unspecified) when the sign bit is set.
– John Bollinger
4 hours ago
@MarcvanLeeuwen, the problem was that it is difficult to assess if there are still such implementations around. And, as always, if people would more engage in committee work, things might go smoother.
– Jens Gustedt
2 hours ago
@JohnBollinger yes, it should be two's complement, see open-std.org/jtc1/sc22/wg14/www/docs/n2330.pdf
– Jens Gustedt
2 hours ago
add a comment |
1
I'm amazed at why language designers seem to be so worried about backwards hardware compatibility. I don't think any computer hardware commercialised during the current century uses anything but twos complement representation, and one can be rather certain that nobody is going to write a compiler for a new language version designed to work on old hardware (the opposite, running an old language version on new hardware, of course happens a lot). So language designers could have assumed twos complement behaviour for a long time, but apparently they are only coming around to doing that just now.
– Marc van Leeuwen
5 hours ago
@Jens, is the committee then considering specifying two's complement representation as the only option? For another way the exotic sign representations might be removed would be to delete the details, but simply make the representations of signed integers implementation defined (or even unspecified) when the sign bit is set.
– John Bollinger
4 hours ago
@MarcvanLeeuwen, the problem was that it is difficult to assess if there are still such implementations around. And, as always, if people would more engage in committee work, things might go smoother.
– Jens Gustedt
2 hours ago
@JohnBollinger yes, it should be two's complement, see open-std.org/jtc1/sc22/wg14/www/docs/n2330.pdf
– Jens Gustedt
2 hours ago
1
1
I'm amazed at why language designers seem to be so worried about backwards hardware compatibility. I don't think any computer hardware commercialised during the current century uses anything but twos complement representation, and one can be rather certain that nobody is going to write a compiler for a new language version designed to work on old hardware (the opposite, running an old language version on new hardware, of course happens a lot). So language designers could have assumed twos complement behaviour for a long time, but apparently they are only coming around to doing that just now.
– Marc van Leeuwen
5 hours ago
I'm amazed at why language designers seem to be so worried about backwards hardware compatibility. I don't think any computer hardware commercialised during the current century uses anything but twos complement representation, and one can be rather certain that nobody is going to write a compiler for a new language version designed to work on old hardware (the opposite, running an old language version on new hardware, of course happens a lot). So language designers could have assumed twos complement behaviour for a long time, but apparently they are only coming around to doing that just now.
– Marc van Leeuwen
5 hours ago
@Jens, is the committee then considering specifying two's complement representation as the only option? For another way the exotic sign representations might be removed would be to delete the details, but simply make the representations of signed integers implementation defined (or even unspecified) when the sign bit is set.
– John Bollinger
4 hours ago
@Jens, is the committee then considering specifying two's complement representation as the only option? For another way the exotic sign representations might be removed would be to delete the details, but simply make the representations of signed integers implementation defined (or even unspecified) when the sign bit is set.
– John Bollinger
4 hours ago
@MarcvanLeeuwen, the problem was that it is difficult to assess if there are still such implementations around. And, as always, if people would more engage in committee work, things might go smoother.
– Jens Gustedt
2 hours ago
@MarcvanLeeuwen, the problem was that it is difficult to assess if there are still such implementations around. And, as always, if people would more engage in committee work, things might go smoother.
– Jens Gustedt
2 hours ago
@JohnBollinger yes, it should be two's complement, see open-std.org/jtc1/sc22/wg14/www/docs/n2330.pdf
– Jens Gustedt
2 hours ago
@JohnBollinger yes, it should be two's complement, see open-std.org/jtc1/sc22/wg14/www/docs/n2330.pdf
– Jens Gustedt
2 hours ago
add a comment |
Your interpretation is correct.
Going up to paragraph 2 of 6.2.6.2:
For signed integer types, the bits of the object
representation shall be divided into three groups: value bits,
padding bits, and the sign bit. There need not be any
padding bits; signed char shall not have any padding bits.
There shall be exactly one sign bit. Each bit that is a
value bit shall have the same value as the same bit in the
object representation of the corresponding unsigned type (if there are
M value bits in the signed type and N in the unsigned type, then M ≤ N
). If the sign bit is zero, it shall not affect the resulting
value. If the sign bit is one, the value shall be modified
in one of the following ways:
- the corresponding value with sign bit 0 is negated ( sign and magnitude );
- the sign bit has the value − (2M)( two’s complement );
- the sign bit has the value − (2M − 1) ( ones’ complement ).
Which of these applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first
two), or with sign bit and all value bits 1 (for ones’
complement), is a trap representation or a normal value. In
the case of sign and magnitude and ones’ complement, if this
representation is a normal value it is called a negative zero.
This means an implementation using either one's complement or sign and magnitude has, for a given size integer type, a specific representation which must be either negative zero or a trap representation. It's then up to the implementation to choose which one of those applies.
As an example, suppose a system has sign and magnitude representation and a 32 bit int
with no padding. Then the representation that would be negative zero, if it is supported, is 0x80000000
.
Now suppose the following operations are performed:
int x = 0x7fffffff;
x = ~x;
If the implementation supports negative zero, the ~
operator will generate -0
as the result and store it in x
. If it does not, it creates a trap representation and invokes undefined behavior as per paragraph 4.
add a comment |
Your interpretation is correct.
Going up to paragraph 2 of 6.2.6.2:
For signed integer types, the bits of the object
representation shall be divided into three groups: value bits,
padding bits, and the sign bit. There need not be any
padding bits; signed char shall not have any padding bits.
There shall be exactly one sign bit. Each bit that is a
value bit shall have the same value as the same bit in the
object representation of the corresponding unsigned type (if there are
M value bits in the signed type and N in the unsigned type, then M ≤ N
). If the sign bit is zero, it shall not affect the resulting
value. If the sign bit is one, the value shall be modified
in one of the following ways:
- the corresponding value with sign bit 0 is negated ( sign and magnitude );
- the sign bit has the value − (2M)( two’s complement );
- the sign bit has the value − (2M − 1) ( ones’ complement ).
Which of these applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first
two), or with sign bit and all value bits 1 (for ones’
complement), is a trap representation or a normal value. In
the case of sign and magnitude and ones’ complement, if this
representation is a normal value it is called a negative zero.
This means an implementation using either one's complement or sign and magnitude has, for a given size integer type, a specific representation which must be either negative zero or a trap representation. It's then up to the implementation to choose which one of those applies.
As an example, suppose a system has sign and magnitude representation and a 32 bit int
with no padding. Then the representation that would be negative zero, if it is supported, is 0x80000000
.
Now suppose the following operations are performed:
int x = 0x7fffffff;
x = ~x;
If the implementation supports negative zero, the ~
operator will generate -0
as the result and store it in x
. If it does not, it creates a trap representation and invokes undefined behavior as per paragraph 4.
add a comment |
Your interpretation is correct.
Going up to paragraph 2 of 6.2.6.2:
For signed integer types, the bits of the object
representation shall be divided into three groups: value bits,
padding bits, and the sign bit. There need not be any
padding bits; signed char shall not have any padding bits.
There shall be exactly one sign bit. Each bit that is a
value bit shall have the same value as the same bit in the
object representation of the corresponding unsigned type (if there are
M value bits in the signed type and N in the unsigned type, then M ≤ N
). If the sign bit is zero, it shall not affect the resulting
value. If the sign bit is one, the value shall be modified
in one of the following ways:
- the corresponding value with sign bit 0 is negated ( sign and magnitude );
- the sign bit has the value − (2M)( two’s complement );
- the sign bit has the value − (2M − 1) ( ones’ complement ).
Which of these applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first
two), or with sign bit and all value bits 1 (for ones’
complement), is a trap representation or a normal value. In
the case of sign and magnitude and ones’ complement, if this
representation is a normal value it is called a negative zero.
This means an implementation using either one's complement or sign and magnitude has, for a given size integer type, a specific representation which must be either negative zero or a trap representation. It's then up to the implementation to choose which one of those applies.
As an example, suppose a system has sign and magnitude representation and a 32 bit int
with no padding. Then the representation that would be negative zero, if it is supported, is 0x80000000
.
Now suppose the following operations are performed:
int x = 0x7fffffff;
x = ~x;
If the implementation supports negative zero, the ~
operator will generate -0
as the result and store it in x
. If it does not, it creates a trap representation and invokes undefined behavior as per paragraph 4.
Your interpretation is correct.
Going up to paragraph 2 of 6.2.6.2:
For signed integer types, the bits of the object
representation shall be divided into three groups: value bits,
padding bits, and the sign bit. There need not be any
padding bits; signed char shall not have any padding bits.
There shall be exactly one sign bit. Each bit that is a
value bit shall have the same value as the same bit in the
object representation of the corresponding unsigned type (if there are
M value bits in the signed type and N in the unsigned type, then M ≤ N
). If the sign bit is zero, it shall not affect the resulting
value. If the sign bit is one, the value shall be modified
in one of the following ways:
- the corresponding value with sign bit 0 is negated ( sign and magnitude );
- the sign bit has the value − (2M)( two’s complement );
- the sign bit has the value − (2M − 1) ( ones’ complement ).
Which of these applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first
two), or with sign bit and all value bits 1 (for ones’
complement), is a trap representation or a normal value. In
the case of sign and magnitude and ones’ complement, if this
representation is a normal value it is called a negative zero.
This means an implementation using either one's complement or sign and magnitude has, for a given size integer type, a specific representation which must be either negative zero or a trap representation. It's then up to the implementation to choose which one of those applies.
As an example, suppose a system has sign and magnitude representation and a 32 bit int
with no padding. Then the representation that would be negative zero, if it is supported, is 0x80000000
.
Now suppose the following operations are performed:
int x = 0x7fffffff;
x = ~x;
If the implementation supports negative zero, the ~
operator will generate -0
as the result and store it in x
. If it does not, it creates a trap representation and invokes undefined behavior as per paragraph 4.
edited 5 hours ago
answered 6 hours ago
dbushdbush
101k13105142
101k13105142
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044324%2fwhat-is-the-meaning-of-producing-negative-zeroes-in-a-system-that-doesnt-supp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you understand by "trap representation" ?
– alinsoar
5 hours ago
I also read lots of times about "tr" but I never saw one concretely, so I cannot understand what that means. I read articles about this, but I do not understand what is a trap representation. On X86 processors are there trs ?
– alinsoar
5 hours ago
1
@alinsoar I don't know of any implementation in the real world that uses them for plain integers (floating point is another story). Mostly it is something invented by C standard committee language lawyers, like they invented the need to support 1's complement and signed magnitude computers.
– Lundin
4 hours ago
1
I don't get why there are complicated answers and why this is even a question, doesn't it just mean exactly what it says? You are asking about a system which does not contain the notion of a negative zero, so isn't that exactly what that section would be describing? Meaning, just as your quoted section says... the behavior that would produce a negative zero (that behavior which doesn't exist in your case) has undefined results. As simple as that. Analogy: Enacting a law saying "Anyone who slays a boogeyman gets a week in jail" is not suddenly confusing simply because of a lack of boogeymen.
– Aaron
34 mins ago