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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:01:37+00:00 2026-06-17T21:01:37+00:00

I have a method to extract the most significant, non-zero byte in an integer

  • 0

I have a method to extract the most significant, non-zero byte in an integer using the following method:

private static int getFirstByte(int n)
{
    while (n > 0xFF)
        n >>= 8;

    return n;
}

There’s a logic problem with this method. The integer parameter could be negative, which means it would return the number being passed in, which is incorrect.

There is also a possible issue with the method itself. It is using a while loop.

Is there a way to perform this logic without a while loop and also possibly avoiding the incorrectly returned result for negative numbers?

  • 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-17T21:01:38+00:00Added an answer on June 17, 2026 at 9:01 pm

    I assume by get the first non-zero byte in an int you mean natural 8 bit breaks of the int and not a dynamic 8 bit break.

    Natural 8 bit breaks:

    00000000|00010110|10110010|11110001 ==> 00010110

    Dynamic 8 bit break:

    00000000000|10110101|1001011110001 ==> 10110101

    This will return the first non-zero byte on a natural 8-bit break of an int without looping or branching. This code may or may not be more efficient then paulsm4‘s answer. Be sure to do benchmarking and/or profiling of the code to determine which is best for you.

    Java Code: ideone link

    class Main {
        public static void main(String[] args) {
            int i,j;
            for (i=0,j=1; i<32; ++i,j<<=1) {
              System.out.printf("0x%08x : 0x%02x\n",j,getByte(j));
            }
        }
        public static byte getByte(int n) {
            int x = n; 
            x |=   (x >>>  1);
            x |=   (x >>>  2);
            x |=   (x >>>  4);
            x |=   (x >>>  8);
            x |=   (x >>> 16);
            x -=  ((x >>>  1) & 0x55555555);
            x  = (((x >>>  2) & 0x33333333) + (x & 0x33333333));
            x  = (((x >>>  4) + x) & 0x0f0f0f0f);
            x +=   (x >>>  8);
            x +=   (x >>> 16);
            x &= 0x0000003f;
            x  = 32 - x;     // x now equals the number of leading zeros
            x &= 0x00000038; // mask out last 3 bits (cause natural byte break)
            return (byte)((n&(0xFF000000>>>x))>>>(24-x));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method like so private bool VerbMethod(string httpVerb, string methodName, string url,
I have a method (getSingleNodeValue()) which when passed an xpatch expression will extract the
Using the zipfile module I have created a script to extract my archived files,
When I have NSString with /Users/user/Projects/thefile.ext I want to extract thefile with Objective-C methods.
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have method with two parameters: bool1 and bool2. Both are boolean of course.
I have method that returns module path of given class name def findModulePath(path, className):
I have method in a class that I need to make sure is only
I've got some 3rd party beans that have method signatures that fit quite well

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.