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 6900371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:32:16+00:00 2026-05-27T07:32:16+00:00

So, I have an array of unsigned chars, currently I’m trying to write a

  • 0

So, I have an array of unsigned chars, currently I’m trying to write a Set method (changes the bit in given index to 1). The best way I could think to do this was instead of creating a mask for the whole array, I would just create a mask the size of a byte and only mask the index spot in the array with the given bit that the user wants to change. However, every way I try to do it, either nothing happens to the resulting array after OR’ing it with a mask of all 0’s with a 1 in the bit index, or I get a seg fault. The best I’ve been able to do is change the correct bit in the first array index. How my code is currently set up right now I understand why it’s only changing the correct bit in the first byte of the array, but every attempt to change this has failed, I don’t think this should be hard I just feel like I’m missing something, but pages of reading and google searches have lead me no where. Here’s a snipit of my code as of now…

void BitArray::Set (unsigned int index)
 70 {
 71     int spot;       // index in barray where 
 72                     // bit to be set is located
 73     char mask;       
 74     if (index < 8)
 75     {   
 76         spot = 0;
 77         mask = 1 >> index - 1;
 78     }   
 79     else
 80     {
 81         int spot = index / 8;
 82         mask = 1 << (index - (8*spot) - 1);
 83     }   
 84     
 85     *barray = *barray | mask;
 86 }   

Instead of the *barray = *barray | mask, I would intuitively want something like barray[spot] = barray[spot] | mask; to work. Any help is greatly appreciated.

  • 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-27T07:32:17+00:00Added an answer on May 27, 2026 at 7:32 am

    I’m not sure why you’re going against your intuitive notion of array[spot] = barray[spot] | mask; And you seem to be making the spot and mask calculations more complicated than necessary.

    Why did you make index < 8 a special case?

    if(index < 8) 
        spot = 0;
    ...
    else
        spot = index / 8;
    

    In both cases index / 8 gives you the right byte index, correct?

    Second, how do either of the following lines give you the right bit position? Why are you shifting right at all? What does spot, the index of the byte you have to access, have to do with the bit position within the byte?

    mask = 1 >> index - 1;
    
    mask = 1 << (index - (8*spot) - 1);
    

    Here’s my untested solution:

    void BitArray::Set(unsigned int index)
    {
        int spot = index / 8;
        char bit = 1 << (index % 8);
        barray[spot] = barray[spot] | bit;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of unsigned chars in c I am trying to print
I have an array of four unsigned chars. I want to treat it like
I have an array of unsigned integers in C and a java array of
A cancer CT picture is stored inside a unsigned short array (1-dimensional). I have
Have an array of chars like char members[255]. How can I empty it completely
I have an unsigned char array that I need in a std::string, but my
I have got an array of the following structs: typedef struct _my_data_ { unsigned
I have done a lot with Java but I am currently trying to learn
Im trying to increase the size of a **array with realloc which I have
I wanted to convert array< Byte>^ to unsigned char*. I have tried to explain

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.