Can I perform bitwise operations on byte[]?
Let's say I have:
byte data = new byte { 1, 212, 29, 144 };
The only way I'm able to figure out to do a bitwise AND & is by first converting the byte to a uint:
if ((BitConverter.ToUInt32(data,0)) & 0x7) == 1)
{
//If the last 3 bits are ...111, then do something
}
This seems ugly. Is there a better way to perform bitwise operations on a byte without having to convert to a UInt? Thanks.
c# bitwise-operators
|
show 1 more comment
Let's say I have:
byte data = new byte { 1, 212, 29, 144 };
The only way I'm able to figure out to do a bitwise AND & is by first converting the byte to a uint:
if ((BitConverter.ToUInt32(data,0)) & 0x7) == 1)
{
//If the last 3 bits are ...111, then do something
}
This seems ugly. Is there a better way to perform bitwise operations on a byte without having to convert to a UInt? Thanks.
c# bitwise-operators
5
data[0] & 0x7
doesn't work ?
– Martin Verjans
Jun 26 '16 at 19:45
@SuperPeanut data[3] would work since I'm wanting to compare the last 3 bits in this example. However, I'm looking for a solution where I can perform an AND operation on a multi-byte value. As another example, I would like to be able to do something like: if (data & 0x80000000) { //Do something true }
– Bert Wagner
Jun 26 '16 at 19:48
Unfortunately, programs are only able to compare things that are the same. Either you convert data intoUInt32
, either you convert the comparer (0x80000000
) into a bit array and do the compare for each item...
– Martin Verjans
Jun 26 '16 at 19:51
What results are you expecting when you logically AND (&
) an array of 13 bytes with a number? Or you are expecting to have an in-built way specifically for byte array of size 4?
– Vikhram
Jun 26 '16 at 19:51
What would you expect the result ofdata & 0x80000000
to mean? What would you expect the result type to be? It's an operation that makes no sense, IMO.
– Jon Skeet
Jun 26 '16 at 19:52
|
show 1 more comment
Let's say I have:
byte data = new byte { 1, 212, 29, 144 };
The only way I'm able to figure out to do a bitwise AND & is by first converting the byte to a uint:
if ((BitConverter.ToUInt32(data,0)) & 0x7) == 1)
{
//If the last 3 bits are ...111, then do something
}
This seems ugly. Is there a better way to perform bitwise operations on a byte without having to convert to a UInt? Thanks.
c# bitwise-operators
Let's say I have:
byte data = new byte { 1, 212, 29, 144 };
The only way I'm able to figure out to do a bitwise AND & is by first converting the byte to a uint:
if ((BitConverter.ToUInt32(data,0)) & 0x7) == 1)
{
//If the last 3 bits are ...111, then do something
}
This seems ugly. Is there a better way to perform bitwise operations on a byte without having to convert to a UInt? Thanks.
c# bitwise-operators
c# bitwise-operators
asked Jun 26 '16 at 19:43
Bert Wagner
621721
621721
5
data[0] & 0x7
doesn't work ?
– Martin Verjans
Jun 26 '16 at 19:45
@SuperPeanut data[3] would work since I'm wanting to compare the last 3 bits in this example. However, I'm looking for a solution where I can perform an AND operation on a multi-byte value. As another example, I would like to be able to do something like: if (data & 0x80000000) { //Do something true }
– Bert Wagner
Jun 26 '16 at 19:48
Unfortunately, programs are only able to compare things that are the same. Either you convert data intoUInt32
, either you convert the comparer (0x80000000
) into a bit array and do the compare for each item...
– Martin Verjans
Jun 26 '16 at 19:51
What results are you expecting when you logically AND (&
) an array of 13 bytes with a number? Or you are expecting to have an in-built way specifically for byte array of size 4?
– Vikhram
Jun 26 '16 at 19:51
What would you expect the result ofdata & 0x80000000
to mean? What would you expect the result type to be? It's an operation that makes no sense, IMO.
– Jon Skeet
Jun 26 '16 at 19:52
|
show 1 more comment
5
data[0] & 0x7
doesn't work ?
– Martin Verjans
Jun 26 '16 at 19:45
@SuperPeanut data[3] would work since I'm wanting to compare the last 3 bits in this example. However, I'm looking for a solution where I can perform an AND operation on a multi-byte value. As another example, I would like to be able to do something like: if (data & 0x80000000) { //Do something true }
– Bert Wagner
Jun 26 '16 at 19:48
Unfortunately, programs are only able to compare things that are the same. Either you convert data intoUInt32
, either you convert the comparer (0x80000000
) into a bit array and do the compare for each item...
– Martin Verjans
Jun 26 '16 at 19:51
What results are you expecting when you logically AND (&
) an array of 13 bytes with a number? Or you are expecting to have an in-built way specifically for byte array of size 4?
– Vikhram
Jun 26 '16 at 19:51
What would you expect the result ofdata & 0x80000000
to mean? What would you expect the result type to be? It's an operation that makes no sense, IMO.
– Jon Skeet
Jun 26 '16 at 19:52
5
5
data[0] & 0x7
doesn't work ?– Martin Verjans
Jun 26 '16 at 19:45
data[0] & 0x7
doesn't work ?– Martin Verjans
Jun 26 '16 at 19:45
@SuperPeanut data[3] would work since I'm wanting to compare the last 3 bits in this example. However, I'm looking for a solution where I can perform an AND operation on a multi-byte value. As another example, I would like to be able to do something like: if (data & 0x80000000) { //Do something true }
– Bert Wagner
Jun 26 '16 at 19:48
@SuperPeanut data[3] would work since I'm wanting to compare the last 3 bits in this example. However, I'm looking for a solution where I can perform an AND operation on a multi-byte value. As another example, I would like to be able to do something like: if (data & 0x80000000) { //Do something true }
– Bert Wagner
Jun 26 '16 at 19:48
Unfortunately, programs are only able to compare things that are the same. Either you convert data into
UInt32
, either you convert the comparer (0x80000000
) into a bit array and do the compare for each item...– Martin Verjans
Jun 26 '16 at 19:51
Unfortunately, programs are only able to compare things that are the same. Either you convert data into
UInt32
, either you convert the comparer (0x80000000
) into a bit array and do the compare for each item...– Martin Verjans
Jun 26 '16 at 19:51
What results are you expecting when you logically AND (
&
) an array of 13 bytes with a number? Or you are expecting to have an in-built way specifically for byte array of size 4?– Vikhram
Jun 26 '16 at 19:51
What results are you expecting when you logically AND (
&
) an array of 13 bytes with a number? Or you are expecting to have an in-built way specifically for byte array of size 4?– Vikhram
Jun 26 '16 at 19:51
What would you expect the result of
data & 0x80000000
to mean? What would you expect the result type to be? It's an operation that makes no sense, IMO.– Jon Skeet
Jun 26 '16 at 19:52
What would you expect the result of
data & 0x80000000
to mean? What would you expect the result type to be? It's an operation that makes no sense, IMO.– Jon Skeet
Jun 26 '16 at 19:52
|
show 1 more comment
2 Answers
2
active
oldest
votes
No, there no direct support in .Net for bit operations on byte arrays.
You can
- convert to existing types like you show in the question
- implement operations on arrays yourself and use arrays
- consider if BigInteger works for your cases (supports all bitwise operation on arbitrary long numbers, but there sitll no direct way to write long constanst outside regular
long
values) - consider if BitArray works for your case (better if you just need to check/set particular bits).
1
Another way is to use BitArray class
– Vikhram
Jun 26 '16 at 19:58
@Vikhram good point, thanks! - inlined in the answer.
– Alexei Levenkov
Jun 26 '16 at 20:10
1
You forgot the unsafe solution.
– Mr Anderson
Jun 26 '16 at 20:33
add a comment |
I found this solution:
byte b1 = 0x11;
byte b2 = 0xF0;
byte b3 = (byte)(b1 & b2);
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%2f38042496%2fcan-i-perform-bitwise-operations-on-byte%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, there no direct support in .Net for bit operations on byte arrays.
You can
- convert to existing types like you show in the question
- implement operations on arrays yourself and use arrays
- consider if BigInteger works for your cases (supports all bitwise operation on arbitrary long numbers, but there sitll no direct way to write long constanst outside regular
long
values) - consider if BitArray works for your case (better if you just need to check/set particular bits).
1
Another way is to use BitArray class
– Vikhram
Jun 26 '16 at 19:58
@Vikhram good point, thanks! - inlined in the answer.
– Alexei Levenkov
Jun 26 '16 at 20:10
1
You forgot the unsafe solution.
– Mr Anderson
Jun 26 '16 at 20:33
add a comment |
No, there no direct support in .Net for bit operations on byte arrays.
You can
- convert to existing types like you show in the question
- implement operations on arrays yourself and use arrays
- consider if BigInteger works for your cases (supports all bitwise operation on arbitrary long numbers, but there sitll no direct way to write long constanst outside regular
long
values) - consider if BitArray works for your case (better if you just need to check/set particular bits).
1
Another way is to use BitArray class
– Vikhram
Jun 26 '16 at 19:58
@Vikhram good point, thanks! - inlined in the answer.
– Alexei Levenkov
Jun 26 '16 at 20:10
1
You forgot the unsafe solution.
– Mr Anderson
Jun 26 '16 at 20:33
add a comment |
No, there no direct support in .Net for bit operations on byte arrays.
You can
- convert to existing types like you show in the question
- implement operations on arrays yourself and use arrays
- consider if BigInteger works for your cases (supports all bitwise operation on arbitrary long numbers, but there sitll no direct way to write long constanst outside regular
long
values) - consider if BitArray works for your case (better if you just need to check/set particular bits).
No, there no direct support in .Net for bit operations on byte arrays.
You can
- convert to existing types like you show in the question
- implement operations on arrays yourself and use arrays
- consider if BigInteger works for your cases (supports all bitwise operation on arbitrary long numbers, but there sitll no direct way to write long constanst outside regular
long
values) - consider if BitArray works for your case (better if you just need to check/set particular bits).
edited Jun 26 '16 at 20:09
answered Jun 26 '16 at 19:56
Alexei Levenkov
84k890132
84k890132
1
Another way is to use BitArray class
– Vikhram
Jun 26 '16 at 19:58
@Vikhram good point, thanks! - inlined in the answer.
– Alexei Levenkov
Jun 26 '16 at 20:10
1
You forgot the unsafe solution.
– Mr Anderson
Jun 26 '16 at 20:33
add a comment |
1
Another way is to use BitArray class
– Vikhram
Jun 26 '16 at 19:58
@Vikhram good point, thanks! - inlined in the answer.
– Alexei Levenkov
Jun 26 '16 at 20:10
1
You forgot the unsafe solution.
– Mr Anderson
Jun 26 '16 at 20:33
1
1
Another way is to use BitArray class
– Vikhram
Jun 26 '16 at 19:58
Another way is to use BitArray class
– Vikhram
Jun 26 '16 at 19:58
@Vikhram good point, thanks! - inlined in the answer.
– Alexei Levenkov
Jun 26 '16 at 20:10
@Vikhram good point, thanks! - inlined in the answer.
– Alexei Levenkov
Jun 26 '16 at 20:10
1
1
You forgot the unsafe solution.
– Mr Anderson
Jun 26 '16 at 20:33
You forgot the unsafe solution.
– Mr Anderson
Jun 26 '16 at 20:33
add a comment |
I found this solution:
byte b1 = 0x11;
byte b2 = 0xF0;
byte b3 = (byte)(b1 & b2);
add a comment |
I found this solution:
byte b1 = 0x11;
byte b2 = 0xF0;
byte b3 = (byte)(b1 & b2);
add a comment |
I found this solution:
byte b1 = 0x11;
byte b2 = 0xF0;
byte b3 = (byte)(b1 & b2);
I found this solution:
byte b1 = 0x11;
byte b2 = 0xF0;
byte b3 = (byte)(b1 & b2);
answered Nov 21 '18 at 16:26
Sunny127
16216
16216
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%2f38042496%2fcan-i-perform-bitwise-operations-on-byte%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
5
data[0] & 0x7
doesn't work ?– Martin Verjans
Jun 26 '16 at 19:45
@SuperPeanut data[3] would work since I'm wanting to compare the last 3 bits in this example. However, I'm looking for a solution where I can perform an AND operation on a multi-byte value. As another example, I would like to be able to do something like: if (data & 0x80000000) { //Do something true }
– Bert Wagner
Jun 26 '16 at 19:48
Unfortunately, programs are only able to compare things that are the same. Either you convert data into
UInt32
, either you convert the comparer (0x80000000
) into a bit array and do the compare for each item...– Martin Verjans
Jun 26 '16 at 19:51
What results are you expecting when you logically AND (
&
) an array of 13 bytes with a number? Or you are expecting to have an in-built way specifically for byte array of size 4?– Vikhram
Jun 26 '16 at 19:51
What would you expect the result of
data & 0x80000000
to mean? What would you expect the result type to be? It's an operation that makes no sense, IMO.– Jon Skeet
Jun 26 '16 at 19:52