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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:53:22+00:00 2026-06-10T20:53:22+00:00

I have 5 bit numbers like 10000 01000 00100 If only one bit is

  • 0

I have 5 bit numbers like

10000
01000
00100

If only one bit is on in my calculation i have no problem.

but if 2 bits are on then I want to select only the first on bit for example

10010

i want to treat it as 2 instead of the number 18

is there any bitwise operation which may i use in such sitution?

  • 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-10T20:53:23+00:00Added an answer on June 10, 2026 at 8:53 pm

    Since you only want to isolate it, not get its index, it’s easy:

    function firstSetBit(number)
    {
        return number & -number;
    }
    

    It works because of the binary representation of -number, which is called "two’s complement".

    To get a better example, let’s say the number is 888, which is 0000001101111000 in binary. The leading zeroes make a 16 bit number, but this works with any integer size.

    To obtain the two’s complement of a number, we first complement it, setting all 1s to 0s and 0s to 1s.

              number: 0000001101111000
          complement: 1111110010000111
    

    Then we add 1 to it.

              number: 0000001101111000
          complement: 1111110010000111
               add 1: 1111110010001000
    

    Note that if the rightmost bit is 1, this would create a carry which flips all 1s into 0s until a 0 is reached.

    This number is now actually also the binary representation of -number.

              number: 0000001101111000
          complement: 1111110010000111
               add 1: 1111110010001000
             -number: 1111110010001000
    

    We now take the bitwise & of number and -number.

              number: 0000001101111000
             -number: 1111110010001000
    number & -number: 0000000000001000
    

    To the right of the target bit, number is all 0s by premise. -number is also all 0s because they got flipped during the +1. Bitwise AND of 0 and 0 produces 0.

    At the target bit, number has a 1, also by premise. -number also has a 1 because of the negate turning it into a 0 and carry putting it back to 1. Bitwise AND of 1 and 1 produces 1.

    To the left of the target bit, number and -number always form 0 and 1 pairs because it is undisturbed by the +1 step of the two’s complement procedure. Bitwise AND of 1 and 0 produces 0.

    And thus, we have shown that number & -number produces the lowest 1 bit of the number.

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

Sidebar

Related Questions

I have a double loop that I not only don't like, but would take
If I have a 32-bit binary number and I want to replace the lower
I have a bit of a theoretical question, however it is a problem I
I have a bit of a problem that I can't seem to code my
I have a bit of a strange problem. I have a music app that
I have a bit of a unique problem with an upcoming project; I need
So, I have had this working on Ubuntu before. But then I upgraded to
I have a PHP variable that looks a bit like this: $id = 01922312;
I have a PHP variable that looks a bit like this: $id = 01922312;
Problem I need to create 32 Bit numbers (signed or unsigned doesn't matter, the

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.