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

1 Answer

0 votes
#include <iostream>
#include <format>

// A petabyte (binary) is:
               
// 1 PiB = 2⁵⁰ bytes = 1,125,899,906,842,624 bytes
// A petabyte (decimal) is:
// 1 PB = 1,000,000,000,000,000 bytes

int main() {
    // Define a petabyte constant (1 PiB = 2^50 bytes)
    constexpr std::size_t PETABYTE = 1ULL << 50;

    // Set buffer size to 2 PiB
    std::size_t buffer_size = 2 * PETABYTE;

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


/* 
run:

Buffer size (2 PiB): 2251799813685248 bytes
PETABYTE (with format): 2,251,799,813,685,248 bytes

*/


 



answered 2 hours ago by avibootz
...