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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:55:50+00:00 2026-06-01T03:55:50+00:00

I am reading a code where it is supposed to implement a bit vector

  • 0

I am reading a code where it is supposed to implement a bit vector using a byte array.
The idea is that the bitvector has the bit set if a number is present at the corresponding position.
E.g. if number 10 is present the bit 10 must be set etc. It is a classic concept and I get it, but I am not sure about the actual implementation.

The part I don’t get is:
bitvector [num / 8] |= 1 << (num % 8);

Where num is the number to set.
If num is 10 then the second byte must be used (num/8 ok so far) but 1 << (num % 8) does not set the second bit of the second byte as it should. Does it?

  • 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-06-01T03:55:51+00:00Added an answer on June 1, 2026 at 3:55 am

    10 % 8 = 2, therefore 1 << (10 % 8) = bit 2, or the value 4 (100 in binary)

    (start counting bits from the right side of the byte, starting at zero). Very simple to verify:

    7 -> 7 % 8 = 7, byte[0], Bit 7 (1 << 7).
    8 -> 8 % 8 = 0, byte[1], Bit 0 (1 << 0).
    9 -> 9 % 8 = 1, byte[1], Bit 1 (1 << 1).
    10 -> 10 % 8 = 2, byte[1], Bit 2 (1 << 2).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some code involving pipes. The idea is that I'm supposed to
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
I`m reading the code of some open source project. I see that in one
I've been using J for a few months now, and I find that reading
I am using C++ and writing a program that is supposed to do a
The problem of reading (with c++ program) binary file generated by fortran code has
I'm just reading Code Complete by Steve McConell and I'm thinking of an Example
I've been reading code complete, not far in yet but one of the things
I have a simple text reading code for Visual Basic: Dim fileReader As String
Reading source code of sample projects, such as Beast and Bort, are recommended as

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.