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

  • Home
  • SEARCH
  • 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 90127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:49:29+00:00 2026-05-10T22:49:29+00:00

As input I get an int (well, actually a string I should convert to

  • 0

As input I get an int (well, actually a string I should convert to an int).
This int should be converted to bits.
For each bit position that has a 1, I should get the position.
In my database, I want all records that have an int value field that has this position as value.
I currently have the following naive code that should ask my entity(holding the databaseValue) if it matches the position, but obviously doesn’t work correctly:

Byte[] bits = BitConverter.GetBytes(theDatabaseValue); return bits[position].equals(1); 

Firstly, I have an array of byte because there apparantly is no bit type. Should I use Boolean[] ? Then, how can I fill this array? Lastly, if previous statements are solved, I should just return bits[position]

I feel like this should somehow be solved with bitmasks, but I don’t know where to start..

Any help would be 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. 2026-05-10T22:49:29+00:00Added an answer on May 10, 2026 at 10:49 pm

    Your feeling is correct. This should be solved with bitmasks. BitConverter does not return bits (and how could it? ‘bits’ isn’t an actual data type), it converts raw bytes to CLR data types. Whenever you want to extract the bits out of something, you should think bitmasks.

    If you want to check if a bit at a certain position is set, use the & operator. Bitwise & is only true if both bits are set. For example if you had two bytes 109 and 33, the result of & would be

       0110 1101 & 0010 0001 -----------   0010 0001

    If you just want to see if a bit is set in an int, you & it with a number that has only the bit you’re checking set (ie 1, 2, 4, 8, 16, 32 and so forth) and check if the result is not zero.

    List<int> BitPositions(uint input) {     List<int> result = new List<int>();     uint mask = 1;     int position = 0;     do {         if (input & mask != 0) {             result.Add(position);         }         mask <<= 1;         position++;     } while (mask != 0);      return result; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get user input in one page, store that in a php
This is my first post here and I wanted to get some input from
I get asked this question a lot and I thought I'd solicit some input
I have this model: public class Package { public string CustomerName { get; set;
I'm having a small bit of trouble trying to get this piece of code
I'm writing a function that takes a string of input from the user until
I'm trying to get console input in my Clojure program, but when it gives
What's the best way to get user input in a C program where the
I'm working on a small project in VB.Net where I get a input from
If I want to get a user input from Console to my Expression Tree.

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.