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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:02:31+00:00 2026-05-30T19:02:31+00:00

this is the numberOfLeadingZeros(long i) in Long.java : public static int numberOfLeadingZeros(long i) {

  • 0

this is the numberOfLeadingZeros(long i) in Long.java :

public static int numberOfLeadingZeros(long i) {
        // HD, Figure 5-6
         if (i == 0)
            return 64;
        int n = 1;
        int x = (int)(i >>> 32);
        if (x == 0) { n += 32; x = (int)i; }
        if (x >>> 16 == 0) { n += 16; x <<= 16; }
        if (x >>> 24 == 0) { n +=  8; x <<=  8; }
        if (x >>> 28 == 0) { n +=  4; x <<=  4; }
        if (x >>> 30 == 0) { n +=  2; x <<=  2; }
        n -= x >>> 31;
        return n;
}

and the author said it is based floor(log2(x)) = 63 – numberOfLeadingZeros(x) or ceil(log2(x)) = 64 – numberOfLeadingZeros(x – 1),is that true? or maybe i am so stupid that can’t understand?

somebody who can explain it to me? thanks!

  • 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-30T19:02:31+00:00Added an answer on May 30, 2026 at 7:02 pm

    The number of digits in a number is floor(log(num)) + 1 (with log in the correct base.) If the total digit space is x, then the number of leading zeros is

    x - (floor(log(num)) + 1) =
    (x - 1) - floor(log(num))
    

    So in your case, with 64 total digits and the number in binary

    leadingZeros(num) = 63 - floor(log_2(num))
    

    Edit:

    >>> is the unsigned right shift operator in Java and << is a left shift. n >>> 32 will shift n 32 bits to the right while filling the empty space with 0s.

    So the algorithm works as follows:

    0000000000000000000000000000000000000000000001011010100000101011
    
    1. Shift i 32 bits to the right so x contains only the upper 32 bits.

      00000000000000000000000000000000

    2. If (a) this is 0, we know the upper 32 bits were 0s so add 32 to the leading zero count and concentrate on the lower 32 bits. (b) Otherwise, this is nonzero so the number has less than 32 leading zeros. Concentrate on the upper 32 bits.

    3. Shift x 16 bits to the right.

      0000000000000101

      By the same process: (a) If this is 0, add 16 to the leading zero count and concentrate on the next lowest 16 bits (by shifting them left into the area where we’re working.) (b) If this is nonzero, then we have less than 16 leading zeros to add.

    4. Repeat for the next byte, then the next 4 bits, 2 bits, and the final bit.

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

Sidebar

Related Questions

This is a bit of a long shot, but if anyone can figure it
This code won't compile because there is an illegal reference to a static field.
This question is related to hooking with Mobile Substrate, but as long as you
This is a long shot, but does anyone know of an algorithm for estimating
this gives me an error, Bad Reciever type 'NSIntegar (aka 'int'). NSString *tempTag =
This is beyond both making sense and my control. That being said here is
This is essentially what I want to do: public abstract class Uniform<T> { public
This is a weired problem, I have implemented simple quick sort as follows.. static
This is my code in C : #include<stdio.h> int main() { printf(hello,world); } When
This was asked by a friend. Strangely enough this java code compiles and runs

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.