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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:50:49+00:00 2026-06-10T03:50:49+00:00

I need to figure out the smallest unsigned integral type that can represent a

  • 0

I need to figure out the smallest unsigned integral type that can represent a particular number, in compile time. Something like this…

//////////////////////////////////////////////////////////////////////////
template<size_t Bits>
struct uint_least{};

template<>
struct uint_least<8>{ typedef std::uint8_t type; };

template<>
struct uint_least<16>{ typedef std::uint16_t type; };

//////////////////////////////////////////////////////////////////////////
template<size_t max>
struct uint_least_bits
{
    static const size_t value = 14; // just a placeholder
};

//////////////////////////////////////////////////////////////////////////
template<size_t max>
class A
{
    typedef typename uint_least<uint_least_bits<max>::value>::type underlying_type;

    underlying_type m_X;
};

uint_least is meant to give you the smallest unsigned integral type that is at least Bits large and it should work for any value up to 64 (not just 8, 16, 32, 64 but also 1, 4, 13, etc).

uint_least_bits is meant to give you the minimum number of bits needed to represent max.

  • How can I implement uint_least?
  • How can I implement uint_least_bits?
  • What types should bits, min, and max be? If the answer is a template type, how can I guard against invalid input?

The exact structuring of the traits doesn’t matter. Feel free to scrap what I provided. I just need to provide a number and get back the smallest unsigned integral type that can hold it.

  • 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-10T03:50:50+00:00Added an answer on June 10, 2026 at 3:50 am

    I did this just yesterday, what a coincidence. I’ll leave it here, even though it’s not exactly what you need(it fixes the best integral type thing anyway):

    #include <type_traits>
    #include <stdint.h>
    
    template<size_t i>
    struct best_type {
        typedef typename std::conditional<
            (i <= 8),
            uint8_t,
            typename std::conditional<
                (i <= 16),
                uint16_t,
                typename std::conditional<
                    (i <= 32),
                    uint32_t,
                    uint64_t
                >::type
            >::type
        >::type type;
    };
    

    Then, you’d use it like this:

    #include <type_traits>
    #include <iostream>
    #include <stdint.h>
    
    template<size_t i>
    struct best_type {
        typedef typename std::conditional<
            (i <= 8),
            uint8_t,
            typename std::conditional<
                (i <= 16),
                uint16_t,
                typename std::conditional<
                    (i <= 32),
                    uint32_t,
                    uint64_t
                >::type
            >::type
        >::type type;
    };   
    
    int main() {
        std::cout << sizeof(best_type<2>::type) << std::endl;
        std::cout << sizeof(best_type<8>::type) << std::endl;
        std::cout << sizeof(best_type<15>::type) << std::endl;
        std::cout << sizeof(best_type<17>::type) << std::endl;
    }
    

    Live demo, here.

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

Sidebar

Related Questions

I need to figure out a way to set ValidateRequest at model...I understand that
So i need to figure out how i can get a record count value
I understand that I need to figure out my own homework, but seeing that
Can't figure out how to use bbp_get_reply_author_role(), and i really need it, returns an
I need to figure out the best way to call a statc method that
I need to figure out which part of a linux program that I am
I need to figure out the number of elements in an Iterable in Java.
I need to figure out how I can give exec permissions to write. var
I need to figure out some clever MySQL snippet that will allow me to
EXAMPLE: http://jsbin.com/ewiko4/5/ So I have some Dynamic divs that I need to figure out

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.