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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:20:14+00:00 2026-06-18T01:20:14+00:00

I was searching about isolating the Right-most bit stuff in binary : And I

  • 0

I was searching about isolating the Right-most bit stuff in binary :

And I got this solution :

y = x & (-x)

so :

    10111100  (x)
&   01000100  (-x)
    --------
    00000100

But now , I want to find the magnitude of a number by finding the left most digit ( not the sign though…)

How can I elaborate the solution of mine to find the most left-bit ?

examples :

10111100

01000100

  • 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-18T01:20:15+00:00Added an answer on June 18, 2026 at 1:20 am

    There’s no similar O(1) bitwise trick to find the magnitude of a number. Many microprocessor instruction sets include a special instruction to “count leading zeroes.” There is no such operator in the C language family which gave JavaScript its bitwise functionality.

    The only O(1) alternative is to use Math.floor( Math.log( n ) / Math.LN2 ) A quick trial of

    for ( var i = 0; i == Math.floor( Math.log( 1<<i ) / Math.LN2 ); ++ i ) ;
    

    gives i == 31 as the result, due to the << operator using 32-bit two’s complement signed arithmetic.

    If you want to be a purist, you can repeatedly right-shift by one, which is O( log n ), or you can repeatedly right-shift by 16 >> i, for i from 0 to 4, rejecting shifts when the result is zero and otherwise accumulating 16 >> i. That is O(log log N) where N is the maximum possible value for n, which means constant time, but in all probability slower than Math.log.

    Code for the O( log log N ) algo:

    var mag = function( n ) {
         var acc = 0;
         for ( var i = 16; i; i >>= 1 ) {
             if ( n >> i ) {
                 n >>= i;
                 acc += i;
             }
         }
         return acc;
    };
    

    Of course, for any of these, you have to left-shift one by the result to obtain the “leftmost 1-bit” rather than an index.

    EDIT: Note, the log based implementation returns -Infinity for zero, whereas the mag function returns 0, which is the same as its result for 1. If you want to account for the possibility of no leftmost 1-bit existing, better to make it a special case.

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

Sidebar

Related Questions

I tried searching about this, but didnt got a solid answer. Is it possible
A bit of introduction to the problem, I tried searching about this on google/stack
I've been googling and searching about but have yet to be successful with this.
I have been searching for about an hour on how to do this in
I've been searching about this topic but i don't seem to find anything concrete
I've spent a couple of hours searching about this error, and I have tested
This is not a programming question per se but a question about searching source
This is some what of a supplementary question to my recent query about searching
I have been searching about this subject now for quite a few days. And
I try searching about this but I just can't find any that can solve

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.