How to set a buffer size to 2 exabytes in C++

1 Answer

0 votes
// An exabyte (binary) is:
               
// 1 EiB = 2 ^ 60 bytes = 1,152,921,504,606,846,976 bytes

#include <iostream>
#include <format>

int main() {
    // Define an exabyte constant (1 EiB = 2^60 bytes)
    constexpr std::size_t EXABYTE = 1ULL << 60;

    // Set buffer size to 2 EiB
    std::size_t buffer_size = 2 * EXABYTE;

    std::cout << "Buffer size (2 EiB): " << buffer_size << " bytes\n";

    // The std::format Approach (C++20)
    // The ':L' tells format to use the locale provided
    std::cout << "Buffer size (2 EiB): "
                  << std::format(std::locale("en_US.UTF-8"), "{:L}", buffer_size) << " bytes\n";
}



/* 
run:

Buffer size (2 EiB): 2305843009213693952 bytes
Buffer size (2 EiB): 2,305,843,009,213,693,952 bytes

*/

 



answered 1 hour ago by avibootz
...