Grabbing 'n' binary bits from end of unsigned int in C? Bit masking?
I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.
Say I want to grab the last 8 bits of an unsigned int value, containing 00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
c binary hex bit bitmask
add a comment |
I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.
Say I want to grab the last 8 bits of an unsigned int value, containing 00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
c binary hex bit bitmask
1
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 '18 at 17:43
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 '18 at 17:52
add a comment |
I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.
Say I want to grab the last 8 bits of an unsigned int value, containing 00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
c binary hex bit bitmask
I'm pretty new to C and I seem to be messing up my bitmasking. From what I understand, it's a way to grab or create something of a subset from a binary value.
Say I want to grab the last 8 bits of an unsigned int value, containing 00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
c binary hex bit bitmask
c binary hex bit bitmask
asked Nov 19 '18 at 17:37
RadiantBytesRadiantBytes
44
44
1
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 '18 at 17:43
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 '18 at 17:52
add a comment |
1
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 '18 at 17:43
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 '18 at 17:52
1
1
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 '18 at 17:43
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 '18 at 17:43
1
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 '18 at 17:52
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 '18 at 17:52
add a comment |
4 Answers
4
active
oldest
votes
Here's a more general solution to generate the mask based on how many bits you're interested int.
unsigned int last_n_bits(unsigned int value, int n)
{
unsigned int mask = -1;
if (n < sizeof(unsigned) * CHAR_BIT)
mask = ((1<<n)-1);
return value & mask;
}
you shouldassert
thatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 '18 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 '18 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 '18 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BIT
as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 '18 at 18:28
add a comment |
You can use the BINARY AND operator for do a binary mask:
unsigned char last_eight_bits = my_number & 0b11111111
2
No need for the bit operation if the target is achar
(or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.
– Swordfish
Nov 19 '18 at 17:45
add a comment |
Mask-out all bits > 0xff
:
value & 0xffu
Or create a mask using(1 << 8) - 1
where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 '18 at 17:50
add a comment |
Say I want to grab the last 8 bits of an unsigned int value, containing
00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
In this case, you would use AND. The following code will grab the 8 least significant bits:
unsigned int number = 0x0F0F0F0Fu;
unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
unsigned int result = number & mask; // result will be 0x0000000F
The &
-Operator is used for an AND-Operation with each bit:
00001111000011110000111100001111
AND 00000000000000000000000011111111
------------------------------------
00000000000000000000000000001111
Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.
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%2f53379951%2fgrabbing-n-binary-bits-from-end-of-unsigned-int-in-c-bit-masking%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a more general solution to generate the mask based on how many bits you're interested int.
unsigned int last_n_bits(unsigned int value, int n)
{
unsigned int mask = -1;
if (n < sizeof(unsigned) * CHAR_BIT)
mask = ((1<<n)-1);
return value & mask;
}
you shouldassert
thatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 '18 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 '18 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 '18 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BIT
as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 '18 at 18:28
add a comment |
Here's a more general solution to generate the mask based on how many bits you're interested int.
unsigned int last_n_bits(unsigned int value, int n)
{
unsigned int mask = -1;
if (n < sizeof(unsigned) * CHAR_BIT)
mask = ((1<<n)-1);
return value & mask;
}
you shouldassert
thatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 '18 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 '18 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 '18 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BIT
as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 '18 at 18:28
add a comment |
Here's a more general solution to generate the mask based on how many bits you're interested int.
unsigned int last_n_bits(unsigned int value, int n)
{
unsigned int mask = -1;
if (n < sizeof(unsigned) * CHAR_BIT)
mask = ((1<<n)-1);
return value & mask;
}
Here's a more general solution to generate the mask based on how many bits you're interested int.
unsigned int last_n_bits(unsigned int value, int n)
{
unsigned int mask = -1;
if (n < sizeof(unsigned) * CHAR_BIT)
mask = ((1<<n)-1);
return value & mask;
}
edited Nov 19 '18 at 18:09
answered Nov 19 '18 at 17:50
cleblanccleblanc
3,4611913
3,4611913
you shouldassert
thatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 '18 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 '18 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 '18 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BIT
as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 '18 at 18:28
add a comment |
you shouldassert
thatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 '18 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 '18 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 '18 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BIT
as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 '18 at 18:28
you should
assert
that n < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 '18 at 17:55
you should
assert
that n < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 '18 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 '18 at 18:03
and then you've got a problem getting all bits.
– Swordfish
Nov 19 '18 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 '18 at 18:11
@Swordfish good valid points.
– cleblanc
Nov 19 '18 at 18:11
The width of unsigned might technically be less than
sizeof(unsigned)*CHAR_BIT
as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.– PSkocik
Nov 19 '18 at 18:28
The width of unsigned might technically be less than
sizeof(unsigned)*CHAR_BIT
as there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.– PSkocik
Nov 19 '18 at 18:28
add a comment |
You can use the BINARY AND operator for do a binary mask:
unsigned char last_eight_bits = my_number & 0b11111111
2
No need for the bit operation if the target is achar
(or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.
– Swordfish
Nov 19 '18 at 17:45
add a comment |
You can use the BINARY AND operator for do a binary mask:
unsigned char last_eight_bits = my_number & 0b11111111
2
No need for the bit operation if the target is achar
(or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.
– Swordfish
Nov 19 '18 at 17:45
add a comment |
You can use the BINARY AND operator for do a binary mask:
unsigned char last_eight_bits = my_number & 0b11111111
You can use the BINARY AND operator for do a binary mask:
unsigned char last_eight_bits = my_number & 0b11111111
answered Nov 19 '18 at 17:43
iEldeniElden
684517
684517
2
No need for the bit operation if the target is achar
(or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.
– Swordfish
Nov 19 '18 at 17:45
add a comment |
2
No need for the bit operation if the target is achar
(or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.
– Swordfish
Nov 19 '18 at 17:45
2
2
No need for the bit operation if the target is a
char
(or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.– Swordfish
Nov 19 '18 at 17:45
No need for the bit operation if the target is a
char
(or another type that is (usually) 8 bits wide). Also 8 times 1 is harder to count than two times f.– Swordfish
Nov 19 '18 at 17:45
add a comment |
Mask-out all bits > 0xff
:
value & 0xffu
Or create a mask using(1 << 8) - 1
where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 '18 at 17:50
add a comment |
Mask-out all bits > 0xff
:
value & 0xffu
Or create a mask using(1 << 8) - 1
where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 '18 at 17:50
add a comment |
Mask-out all bits > 0xff
:
value & 0xffu
Mask-out all bits > 0xff
:
value & 0xffu
answered Nov 19 '18 at 17:44
SwordfishSwordfish
9,52711436
9,52711436
Or create a mask using(1 << 8) - 1
where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 '18 at 17:50
add a comment |
Or create a mask using(1 << 8) - 1
where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 '18 at 17:50
Or create a mask using
(1 << 8) - 1
where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).– Maarten Bodewes
Nov 19 '18 at 17:50
Or create a mask using
(1 << 8) - 1
where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).– Maarten Bodewes
Nov 19 '18 at 17:50
add a comment |
Say I want to grab the last 8 bits of an unsigned int value, containing
00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
In this case, you would use AND. The following code will grab the 8 least significant bits:
unsigned int number = 0x0F0F0F0Fu;
unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
unsigned int result = number & mask; // result will be 0x0000000F
The &
-Operator is used for an AND-Operation with each bit:
00001111000011110000111100001111
AND 00000000000000000000000011111111
------------------------------------
00000000000000000000000000001111
Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.
add a comment |
Say I want to grab the last 8 bits of an unsigned int value, containing
00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
In this case, you would use AND. The following code will grab the 8 least significant bits:
unsigned int number = 0x0F0F0F0Fu;
unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
unsigned int result = number & mask; // result will be 0x0000000F
The &
-Operator is used for an AND-Operation with each bit:
00001111000011110000111100001111
AND 00000000000000000000000011111111
------------------------------------
00000000000000000000000000001111
Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.
add a comment |
Say I want to grab the last 8 bits of an unsigned int value, containing
00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
In this case, you would use AND. The following code will grab the 8 least significant bits:
unsigned int number = 0x0F0F0F0Fu;
unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
unsigned int result = number & mask; // result will be 0x0000000F
The &
-Operator is used for an AND-Operation with each bit:
00001111000011110000111100001111
AND 00000000000000000000000011111111
------------------------------------
00000000000000000000000000001111
Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.
Say I want to grab the last 8 bits of an unsigned int value, containing
00001111000011110000111100001111.
How would I use AND/OR to grab those last 8?
In this case, you would use AND. The following code will grab the 8 least significant bits:
unsigned int number = 0x0F0F0F0Fu;
unsigned int mask = 0x000000FFu; // set all bits to "1" which you want to grab
unsigned int result = number & mask; // result will be 0x0000000F
The &
-Operator is used for an AND-Operation with each bit:
00001111000011110000111100001111
AND 00000000000000000000000011111111
------------------------------------
00000000000000000000000000001111
Be aware that "0 AND X = 0" and that "1 AND X = X". You can do further investigation at: https://en.wikipedia.org/wiki/Boolean_algebra.
answered Nov 19 '18 at 18:18
GoldenArrows777GoldenArrows777
311
311
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%2f53379951%2fgrabbing-n-binary-bits-from-end-of-unsigned-int-in-c-bit-masking%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
1
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 '18 at 17:43
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 '18 at 17:52