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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:35:58+00:00 2026-06-08T11:35:58+00:00

Given an integer value, I need some way to find out the minimum number

  • 0

Given an integer value, I need some way to find out the minimum number of bytes needed to store the value. The value may be signed or unsigned, up to 64-bit. Also take the sign bit into account for signed integers.

For example:

                          8     requires 1 byte at minimum
               unsigned 255     requires 1 byte at minimum
                 signed 255     requires 2 bytes at minimum
                       4351     requires 2 bytes at minimum
                -4294967296     requires 5 bytes at minimum
unsigned 0xFFFFFFFFFFFFFFFF     requires 8 bytes at minimum

I can think of a quick-and-dirty way to solve this, using many if-statements, but there might be better (e.g. simpler, cleverer, faster) ways to do this. You may either assume a method with signature int (long value, bool signed) or two methods int (long value) (for signed) and int (ulong value) (for unsigned).

  • 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-08T11:35:59+00:00Added an answer on June 8, 2026 at 11:35 am

    Let me give it a go at my own question. As far as I can tell, this is a correct solution, but it may not be optimal in speed, conciseness:

    public static int GetMinByteSize(long value, bool signed)
    {
        ulong v = (ulong)value;
        // Invert the value when it is negative.
        if (signed && value < 0)
            v = ~v;
        // The minimum length is 1.
        int length = 1;
        // Is there any bit set in the upper half?
        // Move them to the lower half and try again.
        if ((v & 0xFFFFFFFF00000000) != 0)
        {
            length += 4;
            v >>= 32;
        }
        if ((v & 0xFFFF0000) != 0)
        {
            length += 2;
            v >>= 16;
        }
        if ((v & 0xFF00) != 0)
        {
            length += 1;
            v >>= 8;
        }
        // We have at most 8 bits left.
        // Is the most significant bit set (or cleared for a negative number),
        // then we need an extra byte for the sign bit.
        if (signed && (v & 0x80) != 0)
            length++;
        return length;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given I have an integer value of, e.g., 10 . How can I create
Given a potentially huge integer value (in C# string format), I want to be
Given a string such as: a:2:{i:0;s:1:1;i:1;s:1:2;} I want to find every integer within quotes
I have an Integer array, value[] . It may be of any length. I
Given a set of words, we need to find the anagram words and display
Given an unsorted integer array, and without making any assumptions on the numbers in
Given a distance (50km) as integer: 50 And a time as string in the
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
Given this string random_string= '4' i want to determine if its an integer, a
Given two sorted arrays of integers, a and b , and an integer c

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.