What is the relationship between 2’s complement and IEEE754?
I used to think that all numbers are stored in computers in 2’s complement format. And there is no -0 in 2’s complement. But in IEEE754, there are both +0 and -0. And I read that in JavaScript, all numbers are IEEE754. So now I am confused. What’s more, in JavaScript, ~a = -a -1. I used to think it was caused by the change of 2’s complement of number a. But if every number in JavaScript is stored as IEEE754, how can we convert it as 2’s complement?
javascript floating-point ieee-754 twos-complement
|
show 4 more comments
I used to think that all numbers are stored in computers in 2’s complement format. And there is no -0 in 2’s complement. But in IEEE754, there are both +0 and -0. And I read that in JavaScript, all numbers are IEEE754. So now I am confused. What’s more, in JavaScript, ~a = -a -1. I used to think it was caused by the change of 2’s complement of number a. But if every number in JavaScript is stored as IEEE754, how can we convert it as 2’s complement?
javascript floating-point ieee-754 twos-complement
2's complement is how integer values are represented. Floating-point values are another native format, and the values represent binary exponential notation.
– Pointy
Nov 23 '18 at 18:39
@Pointy Thanks. So is it correct that in JavaScript, integer values are also represented as floating-point values, which are represented as IEEE754?
– T.T
Nov 23 '18 at 18:42
Well JavaScript numbers are always double-precision floating point values. In the course of evaluating subexpressions with boolean operators like~
or|
, 32-bit integer values are used, but as soon as a value is saved in a variable or object property it's always a floating point value.
– Pointy
Nov 23 '18 at 18:46
"So is it correct that in JavaScript, integer values are also represented as floating-point values" – No. there are no integers in JavaScript.
– Jörg W Mittag
Nov 23 '18 at 18:49
3
@JörgWMittag: The text you quoted says “integer values.” JavaScript does have integer values. It uses an IEEE-754 floating-point type to represent numbers, and its format is capable of representing many integer values.
– Eric Postpischil
Nov 24 '18 at 1:28
|
show 4 more comments
I used to think that all numbers are stored in computers in 2’s complement format. And there is no -0 in 2’s complement. But in IEEE754, there are both +0 and -0. And I read that in JavaScript, all numbers are IEEE754. So now I am confused. What’s more, in JavaScript, ~a = -a -1. I used to think it was caused by the change of 2’s complement of number a. But if every number in JavaScript is stored as IEEE754, how can we convert it as 2’s complement?
javascript floating-point ieee-754 twos-complement
I used to think that all numbers are stored in computers in 2’s complement format. And there is no -0 in 2’s complement. But in IEEE754, there are both +0 and -0. And I read that in JavaScript, all numbers are IEEE754. So now I am confused. What’s more, in JavaScript, ~a = -a -1. I used to think it was caused by the change of 2’s complement of number a. But if every number in JavaScript is stored as IEEE754, how can we convert it as 2’s complement?
javascript floating-point ieee-754 twos-complement
javascript floating-point ieee-754 twos-complement
asked Nov 23 '18 at 18:29
T.TT.T
366
366
2's complement is how integer values are represented. Floating-point values are another native format, and the values represent binary exponential notation.
– Pointy
Nov 23 '18 at 18:39
@Pointy Thanks. So is it correct that in JavaScript, integer values are also represented as floating-point values, which are represented as IEEE754?
– T.T
Nov 23 '18 at 18:42
Well JavaScript numbers are always double-precision floating point values. In the course of evaluating subexpressions with boolean operators like~
or|
, 32-bit integer values are used, but as soon as a value is saved in a variable or object property it's always a floating point value.
– Pointy
Nov 23 '18 at 18:46
"So is it correct that in JavaScript, integer values are also represented as floating-point values" – No. there are no integers in JavaScript.
– Jörg W Mittag
Nov 23 '18 at 18:49
3
@JörgWMittag: The text you quoted says “integer values.” JavaScript does have integer values. It uses an IEEE-754 floating-point type to represent numbers, and its format is capable of representing many integer values.
– Eric Postpischil
Nov 24 '18 at 1:28
|
show 4 more comments
2's complement is how integer values are represented. Floating-point values are another native format, and the values represent binary exponential notation.
– Pointy
Nov 23 '18 at 18:39
@Pointy Thanks. So is it correct that in JavaScript, integer values are also represented as floating-point values, which are represented as IEEE754?
– T.T
Nov 23 '18 at 18:42
Well JavaScript numbers are always double-precision floating point values. In the course of evaluating subexpressions with boolean operators like~
or|
, 32-bit integer values are used, but as soon as a value is saved in a variable or object property it's always a floating point value.
– Pointy
Nov 23 '18 at 18:46
"So is it correct that in JavaScript, integer values are also represented as floating-point values" – No. there are no integers in JavaScript.
– Jörg W Mittag
Nov 23 '18 at 18:49
3
@JörgWMittag: The text you quoted says “integer values.” JavaScript does have integer values. It uses an IEEE-754 floating-point type to represent numbers, and its format is capable of representing many integer values.
– Eric Postpischil
Nov 24 '18 at 1:28
2's complement is how integer values are represented. Floating-point values are another native format, and the values represent binary exponential notation.
– Pointy
Nov 23 '18 at 18:39
2's complement is how integer values are represented. Floating-point values are another native format, and the values represent binary exponential notation.
– Pointy
Nov 23 '18 at 18:39
@Pointy Thanks. So is it correct that in JavaScript, integer values are also represented as floating-point values, which are represented as IEEE754?
– T.T
Nov 23 '18 at 18:42
@Pointy Thanks. So is it correct that in JavaScript, integer values are also represented as floating-point values, which are represented as IEEE754?
– T.T
Nov 23 '18 at 18:42
Well JavaScript numbers are always double-precision floating point values. In the course of evaluating subexpressions with boolean operators like
~
or |
, 32-bit integer values are used, but as soon as a value is saved in a variable or object property it's always a floating point value.– Pointy
Nov 23 '18 at 18:46
Well JavaScript numbers are always double-precision floating point values. In the course of evaluating subexpressions with boolean operators like
~
or |
, 32-bit integer values are used, but as soon as a value is saved in a variable or object property it's always a floating point value.– Pointy
Nov 23 '18 at 18:46
"So is it correct that in JavaScript, integer values are also represented as floating-point values" – No. there are no integers in JavaScript.
– Jörg W Mittag
Nov 23 '18 at 18:49
"So is it correct that in JavaScript, integer values are also represented as floating-point values" – No. there are no integers in JavaScript.
– Jörg W Mittag
Nov 23 '18 at 18:49
3
3
@JörgWMittag: The text you quoted says “integer values.” JavaScript does have integer values. It uses an IEEE-754 floating-point type to represent numbers, and its format is capable of representing many integer values.
– Eric Postpischil
Nov 24 '18 at 1:28
@JörgWMittag: The text you quoted says “integer values.” JavaScript does have integer values. It uses an IEEE-754 floating-point type to represent numbers, and its format is capable of representing many integer values.
– Eric Postpischil
Nov 24 '18 at 1:28
|
show 4 more comments
1 Answer
1
active
oldest
votes
JavaScript cheats: It represents numbers using the IEEE-754 basic 64-bit binary floating-point format but converts them to 32-bit integers for bitwise operations and then converts them back.
JavaScript is an implementation of ECMAScript. The ECMAScript 2018 Language Specification (9th edition, June 2018) specifies the unary ~
operator in clause 12.5.8 (shown as 12.5.10 in the table of contents but 12.5.8 in the text!). The relevant part is:
UnaryExpression : ~ UnaryExpression
1. Let expr be the result of evaluating UnaryExpression.
2. Let oldValue be ? ToInt32(? GetValue(expr)).
3. Return the result of applying bitwise complement to oldValue. The result is a signed 32-bit integer.
The ToInt32
operation truncates the number to an integer (removing any fraction part, rounding toward zero) and then maps the number to the interval [−231, +231) modulo 232.
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%2f53451477%2fwhat-is-the-relationship-between-2-s-complement-and-ieee754%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
JavaScript cheats: It represents numbers using the IEEE-754 basic 64-bit binary floating-point format but converts them to 32-bit integers for bitwise operations and then converts them back.
JavaScript is an implementation of ECMAScript. The ECMAScript 2018 Language Specification (9th edition, June 2018) specifies the unary ~
operator in clause 12.5.8 (shown as 12.5.10 in the table of contents but 12.5.8 in the text!). The relevant part is:
UnaryExpression : ~ UnaryExpression
1. Let expr be the result of evaluating UnaryExpression.
2. Let oldValue be ? ToInt32(? GetValue(expr)).
3. Return the result of applying bitwise complement to oldValue. The result is a signed 32-bit integer.
The ToInt32
operation truncates the number to an integer (removing any fraction part, rounding toward zero) and then maps the number to the interval [−231, +231) modulo 232.
add a comment |
JavaScript cheats: It represents numbers using the IEEE-754 basic 64-bit binary floating-point format but converts them to 32-bit integers for bitwise operations and then converts them back.
JavaScript is an implementation of ECMAScript. The ECMAScript 2018 Language Specification (9th edition, June 2018) specifies the unary ~
operator in clause 12.5.8 (shown as 12.5.10 in the table of contents but 12.5.8 in the text!). The relevant part is:
UnaryExpression : ~ UnaryExpression
1. Let expr be the result of evaluating UnaryExpression.
2. Let oldValue be ? ToInt32(? GetValue(expr)).
3. Return the result of applying bitwise complement to oldValue. The result is a signed 32-bit integer.
The ToInt32
operation truncates the number to an integer (removing any fraction part, rounding toward zero) and then maps the number to the interval [−231, +231) modulo 232.
add a comment |
JavaScript cheats: It represents numbers using the IEEE-754 basic 64-bit binary floating-point format but converts them to 32-bit integers for bitwise operations and then converts them back.
JavaScript is an implementation of ECMAScript. The ECMAScript 2018 Language Specification (9th edition, June 2018) specifies the unary ~
operator in clause 12.5.8 (shown as 12.5.10 in the table of contents but 12.5.8 in the text!). The relevant part is:
UnaryExpression : ~ UnaryExpression
1. Let expr be the result of evaluating UnaryExpression.
2. Let oldValue be ? ToInt32(? GetValue(expr)).
3. Return the result of applying bitwise complement to oldValue. The result is a signed 32-bit integer.
The ToInt32
operation truncates the number to an integer (removing any fraction part, rounding toward zero) and then maps the number to the interval [−231, +231) modulo 232.
JavaScript cheats: It represents numbers using the IEEE-754 basic 64-bit binary floating-point format but converts them to 32-bit integers for bitwise operations and then converts them back.
JavaScript is an implementation of ECMAScript. The ECMAScript 2018 Language Specification (9th edition, June 2018) specifies the unary ~
operator in clause 12.5.8 (shown as 12.5.10 in the table of contents but 12.5.8 in the text!). The relevant part is:
UnaryExpression : ~ UnaryExpression
1. Let expr be the result of evaluating UnaryExpression.
2. Let oldValue be ? ToInt32(? GetValue(expr)).
3. Return the result of applying bitwise complement to oldValue. The result is a signed 32-bit integer.
The ToInt32
operation truncates the number to an integer (removing any fraction part, rounding toward zero) and then maps the number to the interval [−231, +231) modulo 232.
answered Nov 24 '18 at 1:33
Eric PostpischilEric Postpischil
74.9k880162
74.9k880162
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%2f53451477%2fwhat-is-the-relationship-between-2-s-complement-and-ieee754%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
2's complement is how integer values are represented. Floating-point values are another native format, and the values represent binary exponential notation.
– Pointy
Nov 23 '18 at 18:39
@Pointy Thanks. So is it correct that in JavaScript, integer values are also represented as floating-point values, which are represented as IEEE754?
– T.T
Nov 23 '18 at 18:42
Well JavaScript numbers are always double-precision floating point values. In the course of evaluating subexpressions with boolean operators like
~
or|
, 32-bit integer values are used, but as soon as a value is saved in a variable or object property it's always a floating point value.– Pointy
Nov 23 '18 at 18:46
"So is it correct that in JavaScript, integer values are also represented as floating-point values" – No. there are no integers in JavaScript.
– Jörg W Mittag
Nov 23 '18 at 18:49
3
@JörgWMittag: The text you quoted says “integer values.” JavaScript does have integer values. It uses an IEEE-754 floating-point type to represent numbers, and its format is capable of representing many integer values.
– Eric Postpischil
Nov 24 '18 at 1:28