Can I perform bitwise operations on byte[]?












1














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.










share|improve this question


















  • 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
















1














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.










share|improve this question


















  • 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














1












1








1







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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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














  • 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








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












2 Answers
2






active

oldest

votes


















1














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).






share|improve this answer



















  • 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














I found this solution:



byte b1 = 0x11;
byte b2 = 0xF0;
byte b3 = (byte)(b1 & b2);





share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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).






    share|improve this answer



















    • 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














    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).






    share|improve this answer



















    • 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








    1






    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).






    share|improve this answer














    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).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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














    • 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













    -1














    I found this solution:



    byte b1 = 0x11;
    byte b2 = 0xF0;
    byte b3 = (byte)(b1 & b2);





    share|improve this answer


























      -1














      I found this solution:



      byte b1 = 0x11;
      byte b2 = 0xF0;
      byte b3 = (byte)(b1 & b2);





      share|improve this answer
























        -1












        -1








        -1






        I found this solution:



        byte b1 = 0x11;
        byte b2 = 0xF0;
        byte b3 = (byte)(b1 & b2);





        share|improve this answer












        I found this solution:



        byte b1 = 0x11;
        byte b2 = 0xF0;
        byte b3 = (byte)(b1 & b2);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 16:26









        Sunny127

        16216




        16216






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'