Grabbing 'n' binary bits from end of unsigned int in C? Bit masking?
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 17:43
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 at 17:52
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 17:37
RadiantBytes
83
83
1
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 at 17:43
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 at 17:52
add a comment |
1
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 at 17:43
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 at 17:52
1
1
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 at 17:43
what have you tried so far ? where are your C code attemtps ?
– LoneWanderer
Nov 19 at 17:43
1
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 at 17:52
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 at 17:52
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
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 shouldassertthatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BITas there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 at 18:28
add a comment |
up vote
0
down vote
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 at 17:45
add a comment |
up vote
0
down vote
Mask-out all bits > 0xff:
value & 0xffu
Or create a mask using(1 << 8) - 1where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 at 17:50
add a comment |
up vote
0
down vote
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 |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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 shouldassertthatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BITas there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 at 18:28
add a comment |
up vote
2
down vote
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 shouldassertthatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BITas there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 at 18:28
add a comment |
up vote
2
down vote
up vote
2
down vote
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 at 18:09
answered Nov 19 at 17:50
cleblanc
3,4161913
3,4161913
you shouldassertthatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BITas there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 at 18:28
add a comment |
you shouldassertthatn < sizeof(unsigned) * CHAR_BIT
– Swordfish
Nov 19 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 at 18:11
The width of unsigned might technically be less thansizeof(unsigned)*CHAR_BITas there could be padding bits, but dealing with that efficiently would lengthen your answer for little practical benefit.
– PSkocik
Nov 19 at 18:28
you should
assert that n < sizeof(unsigned) * CHAR_BIT– Swordfish
Nov 19 at 17:55
you should
assert that n < sizeof(unsigned) * CHAR_BIT– Swordfish
Nov 19 at 17:55
and then you've got a problem getting all bits.
– Swordfish
Nov 19 at 18:03
and then you've got a problem getting all bits.
– Swordfish
Nov 19 at 18:03
@Swordfish good valid points.
– cleblanc
Nov 19 at 18:11
@Swordfish good valid points.
– cleblanc
Nov 19 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 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 at 18:28
add a comment |
up vote
0
down vote
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 at 17:45
add a comment |
up vote
0
down vote
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 at 17:45
add a comment |
up vote
0
down vote
up vote
0
down vote
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 at 17:43
iElden
57917
57917
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 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 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 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 at 17:45
add a comment |
up vote
0
down vote
Mask-out all bits > 0xff:
value & 0xffu
Or create a mask using(1 << 8) - 1where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 at 17:50
add a comment |
up vote
0
down vote
Mask-out all bits > 0xff:
value & 0xffu
Or create a mask using(1 << 8) - 1where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 at 17:50
add a comment |
up vote
0
down vote
up vote
0
down vote
Mask-out all bits > 0xff:
value & 0xffu
Mask-out all bits > 0xff:
value & 0xffu
answered Nov 19 at 17:44
Swordfish
8,64411235
8,64411235
Or create a mask using(1 << 8) - 1where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 at 17:50
add a comment |
Or create a mask using(1 << 8) - 1where 8 represents the number of bits to grab (beware not to exceed the size of the integer, of course).
– Maarten Bodewes
Nov 19 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 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 at 17:50
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 at 18:18
GoldenArrows777
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 at 17:43
1
Possible duplicate of Get last n bits of binary
– Govind Parmar
Nov 19 at 17:52