When I am going to multiply uint32 variable with sint32 variable why I am getting wrong ans?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
sint32 MulDiv (uint32 x_value, sint32 y_value, uint32 z_value) {
sint64 v_Result_value;
sint64 v_Temp_Val;
v_Temp_Val = x_value * y_value;
if (0 == z_value) {
if(v_Temp_Val >= 0) {
v_Result_value = MAX_VAL;
} else {
v_Result_value = MIN_VAL;
}
}
return v_Result_value;
}
If I pass:
- x_value as 4294967295
- y_value as -12
v_Temp_Val become 12, which is not expected.
c embedded
add a comment |
sint32 MulDiv (uint32 x_value, sint32 y_value, uint32 z_value) {
sint64 v_Result_value;
sint64 v_Temp_Val;
v_Temp_Val = x_value * y_value;
if (0 == z_value) {
if(v_Temp_Val >= 0) {
v_Result_value = MAX_VAL;
} else {
v_Result_value = MIN_VAL;
}
}
return v_Result_value;
}
If I pass:
- x_value as 4294967295
- y_value as -12
v_Temp_Val become 12, which is not expected.
c embedded
What ifv_Temp_Val = (x_value * (sint64)y_value);
?
– CristiFati
Jan 3 at 10:00
........and what did you pass as z? Please indent your code clearly to avoid misinterpretation of control flow.
– Martin James
Jan 3 at 10:02
add a comment |
sint32 MulDiv (uint32 x_value, sint32 y_value, uint32 z_value) {
sint64 v_Result_value;
sint64 v_Temp_Val;
v_Temp_Val = x_value * y_value;
if (0 == z_value) {
if(v_Temp_Val >= 0) {
v_Result_value = MAX_VAL;
} else {
v_Result_value = MIN_VAL;
}
}
return v_Result_value;
}
If I pass:
- x_value as 4294967295
- y_value as -12
v_Temp_Val become 12, which is not expected.
c embedded
sint32 MulDiv (uint32 x_value, sint32 y_value, uint32 z_value) {
sint64 v_Result_value;
sint64 v_Temp_Val;
v_Temp_Val = x_value * y_value;
if (0 == z_value) {
if(v_Temp_Val >= 0) {
v_Result_value = MAX_VAL;
} else {
v_Result_value = MIN_VAL;
}
}
return v_Result_value;
}
If I pass:
- x_value as 4294967295
- y_value as -12
v_Temp_Val become 12, which is not expected.
c embedded
c embedded
edited Jan 3 at 10:05
CristiFati
15k72538
15k72538
asked Jan 3 at 9:53


TejasTejas
126
126
What ifv_Temp_Val = (x_value * (sint64)y_value);
?
– CristiFati
Jan 3 at 10:00
........and what did you pass as z? Please indent your code clearly to avoid misinterpretation of control flow.
– Martin James
Jan 3 at 10:02
add a comment |
What ifv_Temp_Val = (x_value * (sint64)y_value);
?
– CristiFati
Jan 3 at 10:00
........and what did you pass as z? Please indent your code clearly to avoid misinterpretation of control flow.
– Martin James
Jan 3 at 10:02
What if
v_Temp_Val = (x_value * (sint64)y_value);
?– CristiFati
Jan 3 at 10:00
What if
v_Temp_Val = (x_value * (sint64)y_value);
?– CristiFati
Jan 3 at 10:00
........and what did you pass as z? Please indent your code clearly to avoid misinterpretation of control flow.
– Martin James
Jan 3 at 10:02
........and what did you pass as z? Please indent your code clearly to avoid misinterpretation of control flow.
– Martin James
Jan 3 at 10:02
add a comment |
1 Answer
1
active
oldest
votes
With the expression x_value * y_value
, the values of x_value
and y_value
goes through Usual Arithmetic Conversion, which
Otherwise, the signedness is different: If the operand with the unsigned type has conversion rank greater or equal than the rank of the type of the signed operand, then the operand with the signed type is implicitly converted to the unsigned type
That is, the value -12
is "converted" to an unsigned value, and due to how two's complement (the most common way to handle negative numbers on computers) works the value -12
is converted to a very large value (4294967284
more precisely).
Multiplying 4294967295
by 4294967284
result in arithmetic overflow (because it's a 32-bit multiplication with a 32-bit result), but it's well-defined for unsigned integers. However the result will not be what you expect.
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%2f54019861%2fwhen-i-am-going-to-multiply-uint32-variable-with-sint32-variable-why-i-am-gettin%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
With the expression x_value * y_value
, the values of x_value
and y_value
goes through Usual Arithmetic Conversion, which
Otherwise, the signedness is different: If the operand with the unsigned type has conversion rank greater or equal than the rank of the type of the signed operand, then the operand with the signed type is implicitly converted to the unsigned type
That is, the value -12
is "converted" to an unsigned value, and due to how two's complement (the most common way to handle negative numbers on computers) works the value -12
is converted to a very large value (4294967284
more precisely).
Multiplying 4294967295
by 4294967284
result in arithmetic overflow (because it's a 32-bit multiplication with a 32-bit result), but it's well-defined for unsigned integers. However the result will not be what you expect.
add a comment |
With the expression x_value * y_value
, the values of x_value
and y_value
goes through Usual Arithmetic Conversion, which
Otherwise, the signedness is different: If the operand with the unsigned type has conversion rank greater or equal than the rank of the type of the signed operand, then the operand with the signed type is implicitly converted to the unsigned type
That is, the value -12
is "converted" to an unsigned value, and due to how two's complement (the most common way to handle negative numbers on computers) works the value -12
is converted to a very large value (4294967284
more precisely).
Multiplying 4294967295
by 4294967284
result in arithmetic overflow (because it's a 32-bit multiplication with a 32-bit result), but it's well-defined for unsigned integers. However the result will not be what you expect.
add a comment |
With the expression x_value * y_value
, the values of x_value
and y_value
goes through Usual Arithmetic Conversion, which
Otherwise, the signedness is different: If the operand with the unsigned type has conversion rank greater or equal than the rank of the type of the signed operand, then the operand with the signed type is implicitly converted to the unsigned type
That is, the value -12
is "converted" to an unsigned value, and due to how two's complement (the most common way to handle negative numbers on computers) works the value -12
is converted to a very large value (4294967284
more precisely).
Multiplying 4294967295
by 4294967284
result in arithmetic overflow (because it's a 32-bit multiplication with a 32-bit result), but it's well-defined for unsigned integers. However the result will not be what you expect.
With the expression x_value * y_value
, the values of x_value
and y_value
goes through Usual Arithmetic Conversion, which
Otherwise, the signedness is different: If the operand with the unsigned type has conversion rank greater or equal than the rank of the type of the signed operand, then the operand with the signed type is implicitly converted to the unsigned type
That is, the value -12
is "converted" to an unsigned value, and due to how two's complement (the most common way to handle negative numbers on computers) works the value -12
is converted to a very large value (4294967284
more precisely).
Multiplying 4294967295
by 4294967284
result in arithmetic overflow (because it's a 32-bit multiplication with a 32-bit result), but it's well-defined for unsigned integers. However the result will not be what you expect.
answered Jan 3 at 10:06


Some programmer dudeSome programmer dude
305k25265427
305k25265427
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%2f54019861%2fwhen-i-am-going-to-multiply-uint32-variable-with-sint32-variable-why-i-am-gettin%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 if
v_Temp_Val = (x_value * (sint64)y_value);
?– CristiFati
Jan 3 at 10:00
........and what did you pass as z? Please indent your code clearly to avoid misinterpretation of control flow.
– Martin James
Jan 3 at 10:02