Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:15:34+00:00 2026-05-25T15:15:34+00:00

I have a byte array of 32 bytes where the first 4 bits of

  • 0

I have a byte array of 32 bytes where the first 4 bits of each byte (0 to 3) represent the set or unset state of a number between 1 and 128. For example, If I am given the number 3, I need to set bit 2 in the first byte in the array. If I am given the number 9, I need to set bit 0 of the third byte in the array. The problem I have is finding a sensible way to do this in C#. I’m sure there must be a simple way to do it mathematically but so far haven’t been able to find a method. Whilst I scratch my head on this one I thought I’d see if anyone can give some advice.

——— Update ——————-

Based on the answers given, I have produced the following function. This does exactly what I need. I may not have made it clear in my question what I needed but enough advice was given for me to find the right code.

// outputNumber = number passed into this function

byte[] bytes = new byte[32];

int bit = (outputNumber - 1) % 4;

byte byteSetting = (byte)(1 << bit);

bytes[(outputNumber - 1) / 4] |= byteSetting;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:15:35+00:00Added an answer on May 25, 2026 at 3:15 pm
    int byt = bitNumber / 4; // You could do byt = bitNumber >> 2
    int bit = bitNumber % 4; // You could do bit = bitNumber & 3
    
    bytes[byt] |= (byte)(1 << bit);
    

    Where bytes is your byte array.

    To reset it:

    bytes[byt] &= (byte)(byte.MaxValue ^ (1 << bit));
    

    To read the value of a byte:

    var res = bytes[byt] & (byte)(1 << bit)
    

    (if you are interested, ^ is the xor operator)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte array of around 10,000 bytes which is basically a blob
I have a byte array that may or may not have null bytes at
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
I have a byte[] array, the contents of which represent a TIFF file (as
I have a byte array I want to assign as follows: First byte specifies
Suppose I have a Byte array and I use Encoding.ASCII.GetString() to convert the bytes
The first 3 bytes of a byte array are just integers, is there a
Now I have an array of 7 bits bytes or a string I want
I have an object with a char Array; where the first 5 bytes(char in
I have byte array as input. I would like to convert that array to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.