Unexpected result with right shift after bitwise negation Announcing the arrival of Valued...
Why use gamma over alpha radiation?
Is it possible to ask for a hotel room without minibar/extra services?
Why is there no army of Iron-Mans in the MCU?
What kind of display is this?
How are presidential pardons supposed to be used?
Who can trigger ship-wide alerts in Star Trek?
Stars Make Stars
How to market an anarchic city as a tourism spot to people living in civilized areas?
New Order #5: where Fibonacci and Beatty meet at Wythoff
Autumning in love
How do you clear the ApexPages.getMessages() collection in a test?
Stop battery usage [Ubuntu 18]
What did Darwin mean by 'squib' here?
Array/tabular for long multiplication
Can I throw a sword that doesn't have the Thrown property at someone?
What do you call a plan that's an alternative plan in case your initial plan fails?
Working around an AWS network ACL rule limit
How should I respond to a player wanting to catch a sword between their hands?
When communicating altitude with a '9' in it, should it be pronounced "nine hundred" or "niner hundred"?
What would be Julian Assange's expected punishment, on the current English criminal law?
What LEGO pieces have "real-world" functionality?
Are my PIs rude or am I just being too sensitive?
I'm having difficulty getting my players to do stuff in a sandbox campaign
How is simplicity better than precision and clarity in prose?
Unexpected result with right shift after bitwise negation
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceWhat are bitwise shift (bit-shift) operators and how do they work?Improve INSERT-per-second performance of SQLite?Right shift two's complement number like an unsigned intbit shifting in C, unexpected resultRight shift with zeros at the beginningUnexepected behavior from multiple bitwise shifts on the same lineUnexpected Result After Arithmetically Right ShiftingWhy unsigned int right shift is always filled with '1'Unusual behavior with shift-right bitwise operatorprintf() function in loop #3 gives unexpected result
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I expected that below code will output 10
because (~port)
equal to 10100101
So, when we right shift it by 4
we get 00001010
which is 10
.
But the output is 250
! Why?
int main()
{
uint8_t port = 0x5a;
uint8_t result_8 = (~port) >> 4;
//result_8 = result_8 >> 4;
printf("%i", result_8);
return 0;
}
c bit-manipulation
add a comment |
I expected that below code will output 10
because (~port)
equal to 10100101
So, when we right shift it by 4
we get 00001010
which is 10
.
But the output is 250
! Why?
int main()
{
uint8_t port = 0x5a;
uint8_t result_8 = (~port) >> 4;
//result_8 = result_8 >> 4;
printf("%i", result_8);
return 0;
}
c bit-manipulation
add a comment |
I expected that below code will output 10
because (~port)
equal to 10100101
So, when we right shift it by 4
we get 00001010
which is 10
.
But the output is 250
! Why?
int main()
{
uint8_t port = 0x5a;
uint8_t result_8 = (~port) >> 4;
//result_8 = result_8 >> 4;
printf("%i", result_8);
return 0;
}
c bit-manipulation
I expected that below code will output 10
because (~port)
equal to 10100101
So, when we right shift it by 4
we get 00001010
which is 10
.
But the output is 250
! Why?
int main()
{
uint8_t port = 0x5a;
uint8_t result_8 = (~port) >> 4;
//result_8 = result_8 >> 4;
printf("%i", result_8);
return 0;
}
c bit-manipulation
c bit-manipulation
edited 22 mins ago
John Kugelman
249k54407460
249k54407460
asked 38 mins ago
IslamIslam
545
545
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
C promotes uint8_t
to int
before doing operations on it. So:
port
is promoted to signed integer0x0000005a
.
~
inverts it giving0xffffffa5
.- An arithmetic shift returns
0xfffffffa
. - It's truncated back into a
uint8_t
giving0xfa == 250
.
To fix that, either truncate the temporary result:
uint8_t result_8 = (uint8_t)(~port) >> 4;
or mask it:
uint8_t result_8 = (~port & 0xff) >> 4;
you're right but i think C doesn't promote onlyuint8_t
but alsounsigned char
because i tested it withunsigned char
too and got the same result! Am i right?
– Islam
20 mins ago
2
uint8_t
is, very likely, a synonym ofunsigned char
on your system. The promotion rules apply to all integral types smaller thanint
.
– ybungalobill
18 mins ago
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%2f55681351%2funexpected-result-with-right-shift-after-bitwise-negation%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
C promotes uint8_t
to int
before doing operations on it. So:
port
is promoted to signed integer0x0000005a
.
~
inverts it giving0xffffffa5
.- An arithmetic shift returns
0xfffffffa
. - It's truncated back into a
uint8_t
giving0xfa == 250
.
To fix that, either truncate the temporary result:
uint8_t result_8 = (uint8_t)(~port) >> 4;
or mask it:
uint8_t result_8 = (~port & 0xff) >> 4;
you're right but i think C doesn't promote onlyuint8_t
but alsounsigned char
because i tested it withunsigned char
too and got the same result! Am i right?
– Islam
20 mins ago
2
uint8_t
is, very likely, a synonym ofunsigned char
on your system. The promotion rules apply to all integral types smaller thanint
.
– ybungalobill
18 mins ago
add a comment |
C promotes uint8_t
to int
before doing operations on it. So:
port
is promoted to signed integer0x0000005a
.
~
inverts it giving0xffffffa5
.- An arithmetic shift returns
0xfffffffa
. - It's truncated back into a
uint8_t
giving0xfa == 250
.
To fix that, either truncate the temporary result:
uint8_t result_8 = (uint8_t)(~port) >> 4;
or mask it:
uint8_t result_8 = (~port & 0xff) >> 4;
you're right but i think C doesn't promote onlyuint8_t
but alsounsigned char
because i tested it withunsigned char
too and got the same result! Am i right?
– Islam
20 mins ago
2
uint8_t
is, very likely, a synonym ofunsigned char
on your system. The promotion rules apply to all integral types smaller thanint
.
– ybungalobill
18 mins ago
add a comment |
C promotes uint8_t
to int
before doing operations on it. So:
port
is promoted to signed integer0x0000005a
.
~
inverts it giving0xffffffa5
.- An arithmetic shift returns
0xfffffffa
. - It's truncated back into a
uint8_t
giving0xfa == 250
.
To fix that, either truncate the temporary result:
uint8_t result_8 = (uint8_t)(~port) >> 4;
or mask it:
uint8_t result_8 = (~port & 0xff) >> 4;
C promotes uint8_t
to int
before doing operations on it. So:
port
is promoted to signed integer0x0000005a
.
~
inverts it giving0xffffffa5
.- An arithmetic shift returns
0xfffffffa
. - It's truncated back into a
uint8_t
giving0xfa == 250
.
To fix that, either truncate the temporary result:
uint8_t result_8 = (uint8_t)(~port) >> 4;
or mask it:
uint8_t result_8 = (~port & 0xff) >> 4;
answered 35 mins ago
ybungalobillybungalobill
46.1k1395162
46.1k1395162
you're right but i think C doesn't promote onlyuint8_t
but alsounsigned char
because i tested it withunsigned char
too and got the same result! Am i right?
– Islam
20 mins ago
2
uint8_t
is, very likely, a synonym ofunsigned char
on your system. The promotion rules apply to all integral types smaller thanint
.
– ybungalobill
18 mins ago
add a comment |
you're right but i think C doesn't promote onlyuint8_t
but alsounsigned char
because i tested it withunsigned char
too and got the same result! Am i right?
– Islam
20 mins ago
2
uint8_t
is, very likely, a synonym ofunsigned char
on your system. The promotion rules apply to all integral types smaller thanint
.
– ybungalobill
18 mins ago
you're right but i think C doesn't promote only
uint8_t
but also unsigned char
because i tested it with unsigned char
too and got the same result! Am i right?– Islam
20 mins ago
you're right but i think C doesn't promote only
uint8_t
but also unsigned char
because i tested it with unsigned char
too and got the same result! Am i right?– Islam
20 mins ago
2
2
uint8_t
is, very likely, a synonym of unsigned char
on your system. The promotion rules apply to all integral types smaller than int
.– ybungalobill
18 mins ago
uint8_t
is, very likely, a synonym of unsigned char
on your system. The promotion rules apply to all integral types smaller than int
.– ybungalobill
18 mins ago
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%2f55681351%2funexpected-result-with-right-shift-after-bitwise-negation%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