How to strip off left side of binary number in Python?
I got this binary number 101111111111000
I need to strip off the 8 most significant bits and have 11111000
at the end.
I tried to make 101111111111000 << 8
, but this results in 10111111111100000000000
, it hasn't the same effect as >>
which strips the lower bits. So how can this be done? The final result MUST BE binary type.
python binary
|
show 1 more comment
I got this binary number 101111111111000
I need to strip off the 8 most significant bits and have 11111000
at the end.
I tried to make 101111111111000 << 8
, but this results in 10111111111100000000000
, it hasn't the same effect as >>
which strips the lower bits. So how can this be done? The final result MUST BE binary type.
python binary
2
What do you mean by "binary type"? Python has no such type. Would an integer suffice?
– Rory Daulton
Nov 25 '18 at 22:39
"it hasn't the same effect as >> which strips the lower bits" is because>>
does not "strip", but it shifts the bits. So it does have the same effect as<<
, in the other direction.
– usr2564301
Nov 25 '18 at 22:40
Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.
– Rodrigo Formighieri
Nov 25 '18 at 22:41
1
@Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.
– Rodrigo Formighieri
Nov 25 '18 at 22:52
1
@RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.
– Alnitak
Nov 25 '18 at 23:00
|
show 1 more comment
I got this binary number 101111111111000
I need to strip off the 8 most significant bits and have 11111000
at the end.
I tried to make 101111111111000 << 8
, but this results in 10111111111100000000000
, it hasn't the same effect as >>
which strips the lower bits. So how can this be done? The final result MUST BE binary type.
python binary
I got this binary number 101111111111000
I need to strip off the 8 most significant bits and have 11111000
at the end.
I tried to make 101111111111000 << 8
, but this results in 10111111111100000000000
, it hasn't the same effect as >>
which strips the lower bits. So how can this be done? The final result MUST BE binary type.
python binary
python binary
asked Nov 25 '18 at 22:35
Rodrigo FormighieriRodrigo Formighieri
1109
1109
2
What do you mean by "binary type"? Python has no such type. Would an integer suffice?
– Rory Daulton
Nov 25 '18 at 22:39
"it hasn't the same effect as >> which strips the lower bits" is because>>
does not "strip", but it shifts the bits. So it does have the same effect as<<
, in the other direction.
– usr2564301
Nov 25 '18 at 22:40
Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.
– Rodrigo Formighieri
Nov 25 '18 at 22:41
1
@Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.
– Rodrigo Formighieri
Nov 25 '18 at 22:52
1
@RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.
– Alnitak
Nov 25 '18 at 23:00
|
show 1 more comment
2
What do you mean by "binary type"? Python has no such type. Would an integer suffice?
– Rory Daulton
Nov 25 '18 at 22:39
"it hasn't the same effect as >> which strips the lower bits" is because>>
does not "strip", but it shifts the bits. So it does have the same effect as<<
, in the other direction.
– usr2564301
Nov 25 '18 at 22:40
Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.
– Rodrigo Formighieri
Nov 25 '18 at 22:41
1
@Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.
– Rodrigo Formighieri
Nov 25 '18 at 22:52
1
@RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.
– Alnitak
Nov 25 '18 at 23:00
2
2
What do you mean by "binary type"? Python has no such type. Would an integer suffice?
– Rory Daulton
Nov 25 '18 at 22:39
What do you mean by "binary type"? Python has no such type. Would an integer suffice?
– Rory Daulton
Nov 25 '18 at 22:39
"it hasn't the same effect as >> which strips the lower bits" is because
>>
does not "strip", but it shifts the bits. So it does have the same effect as <<
, in the other direction.– usr2564301
Nov 25 '18 at 22:40
"it hasn't the same effect as >> which strips the lower bits" is because
>>
does not "strip", but it shifts the bits. So it does have the same effect as <<
, in the other direction.– usr2564301
Nov 25 '18 at 22:40
Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.
– Rodrigo Formighieri
Nov 25 '18 at 22:41
Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.
– Rodrigo Formighieri
Nov 25 '18 at 22:41
1
1
@Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.
– Rodrigo Formighieri
Nov 25 '18 at 22:52
@Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.
– Rodrigo Formighieri
Nov 25 '18 at 22:52
1
1
@RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.
– Alnitak
Nov 25 '18 at 23:00
@RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.
– Alnitak
Nov 25 '18 at 23:00
|
show 1 more comment
3 Answers
3
active
oldest
votes
You can get the rightmost 8 bits of an integer n
using bitwise-and:
n&255
255 is the sum of the values of the smallest 8 bits.
So, starting with 101111111111000
, which is 24568 decimal, n&255
gives 11111000
, which is 248 decimal.
this is only consistent with the OP's question if the input data had 16 bits to start with.
– Alnitak
Nov 25 '18 at 22:44
Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.
– khelwood
Nov 25 '18 at 22:49
add a comment |
another general way to do this is:
n = int('101111111111000', 2) # 24568
n & (1 << int.bit_length(n) - 8) - 1
gives 120 (or 1111000
)
Also works! Thanks
– Rodrigo Formighieri
Nov 26 '18 at 12:43
add a comment |
To achieve this for a number x
with n
digits, one can use this
x&(2**(len(bin(x))-2-8)-1)
-2 to strip 0b, -8 to strip leftmost
Simply said it ands
your number with just enough 1
s that the 8 leftmost bits are set to 0.
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%2f53472702%2fhow-to-strip-off-left-side-of-binary-number-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can get the rightmost 8 bits of an integer n
using bitwise-and:
n&255
255 is the sum of the values of the smallest 8 bits.
So, starting with 101111111111000
, which is 24568 decimal, n&255
gives 11111000
, which is 248 decimal.
this is only consistent with the OP's question if the input data had 16 bits to start with.
– Alnitak
Nov 25 '18 at 22:44
Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.
– khelwood
Nov 25 '18 at 22:49
add a comment |
You can get the rightmost 8 bits of an integer n
using bitwise-and:
n&255
255 is the sum of the values of the smallest 8 bits.
So, starting with 101111111111000
, which is 24568 decimal, n&255
gives 11111000
, which is 248 decimal.
this is only consistent with the OP's question if the input data had 16 bits to start with.
– Alnitak
Nov 25 '18 at 22:44
Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.
– khelwood
Nov 25 '18 at 22:49
add a comment |
You can get the rightmost 8 bits of an integer n
using bitwise-and:
n&255
255 is the sum of the values of the smallest 8 bits.
So, starting with 101111111111000
, which is 24568 decimal, n&255
gives 11111000
, which is 248 decimal.
You can get the rightmost 8 bits of an integer n
using bitwise-and:
n&255
255 is the sum of the values of the smallest 8 bits.
So, starting with 101111111111000
, which is 24568 decimal, n&255
gives 11111000
, which is 248 decimal.
edited Nov 25 '18 at 22:44
answered Nov 25 '18 at 22:38
khelwoodkhelwood
31.6k74365
31.6k74365
this is only consistent with the OP's question if the input data had 16 bits to start with.
– Alnitak
Nov 25 '18 at 22:44
Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.
– khelwood
Nov 25 '18 at 22:49
add a comment |
this is only consistent with the OP's question if the input data had 16 bits to start with.
– Alnitak
Nov 25 '18 at 22:44
Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.
– khelwood
Nov 25 '18 at 22:49
this is only consistent with the OP's question if the input data had 16 bits to start with.
– Alnitak
Nov 25 '18 at 22:44
this is only consistent with the OP's question if the input data had 16 bits to start with.
– Alnitak
Nov 25 '18 at 22:44
Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.
– khelwood
Nov 25 '18 at 22:49
Well possibly. The OP said they wanted to strip the first 8 bits off a 15 bit number, but the output they asked for was 8 bits, so it doesn't seem like that was quite what they wanted.
– khelwood
Nov 25 '18 at 22:49
add a comment |
another general way to do this is:
n = int('101111111111000', 2) # 24568
n & (1 << int.bit_length(n) - 8) - 1
gives 120 (or 1111000
)
Also works! Thanks
– Rodrigo Formighieri
Nov 26 '18 at 12:43
add a comment |
another general way to do this is:
n = int('101111111111000', 2) # 24568
n & (1 << int.bit_length(n) - 8) - 1
gives 120 (or 1111000
)
Also works! Thanks
– Rodrigo Formighieri
Nov 26 '18 at 12:43
add a comment |
another general way to do this is:
n = int('101111111111000', 2) # 24568
n & (1 << int.bit_length(n) - 8) - 1
gives 120 (or 1111000
)
another general way to do this is:
n = int('101111111111000', 2) # 24568
n & (1 << int.bit_length(n) - 8) - 1
gives 120 (or 1111000
)
answered Nov 25 '18 at 23:18
Sam MasonSam Mason
3,34711331
3,34711331
Also works! Thanks
– Rodrigo Formighieri
Nov 26 '18 at 12:43
add a comment |
Also works! Thanks
– Rodrigo Formighieri
Nov 26 '18 at 12:43
Also works! Thanks
– Rodrigo Formighieri
Nov 26 '18 at 12:43
Also works! Thanks
– Rodrigo Formighieri
Nov 26 '18 at 12:43
add a comment |
To achieve this for a number x
with n
digits, one can use this
x&(2**(len(bin(x))-2-8)-1)
-2 to strip 0b, -8 to strip leftmost
Simply said it ands
your number with just enough 1
s that the 8 leftmost bits are set to 0.
add a comment |
To achieve this for a number x
with n
digits, one can use this
x&(2**(len(bin(x))-2-8)-1)
-2 to strip 0b, -8 to strip leftmost
Simply said it ands
your number with just enough 1
s that the 8 leftmost bits are set to 0.
add a comment |
To achieve this for a number x
with n
digits, one can use this
x&(2**(len(bin(x))-2-8)-1)
-2 to strip 0b, -8 to strip leftmost
Simply said it ands
your number with just enough 1
s that the 8 leftmost bits are set to 0.
To achieve this for a number x
with n
digits, one can use this
x&(2**(len(bin(x))-2-8)-1)
-2 to strip 0b, -8 to strip leftmost
Simply said it ands
your number with just enough 1
s that the 8 leftmost bits are set to 0.
answered Nov 25 '18 at 23:05
Jonathan RJonathan R
1,142512
1,142512
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%2f53472702%2fhow-to-strip-off-left-side-of-binary-number-in-python%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
What do you mean by "binary type"? Python has no such type. Would an integer suffice?
– Rory Daulton
Nov 25 '18 at 22:39
"it hasn't the same effect as >> which strips the lower bits" is because
>>
does not "strip", but it shifts the bits. So it does have the same effect as<<
, in the other direction.– usr2564301
Nov 25 '18 at 22:40
Integers are binary yeah, but I'am working in the binary level, that's why i'am approaching it with binaries.
– Rodrigo Formighieri
Nov 25 '18 at 22:41
1
@Alnitak It would be more like I need the 8 lower significant bits of any binary number, doesnt matter the word length.
– Rodrigo Formighieri
Nov 25 '18 at 22:52
1
@RodrigoFormighieri that's a completely different (but far easier) question to answer. Your question would have made more sense if you had described what you wanted to retain rather than what you want to throw away.
– Alnitak
Nov 25 '18 at 23:00