64 bit C question

Jerry Feldman gaf at blu.org
Wed Feb 18 08:28:55 EST 2009


On 02/18/2009 07:43 AM, Jerry Feldman wrote:
> On 02/17/2009 05:30 PM, bruce.labitt at autoliv.com wrote:
>> Is there some way to to compile a C or C++ program that will output a 
>> 64bit hex value correctly?  This is what I have so far...
>>   
> In C++:
> #include <iostream>
>
> #include<cmath>
>
> int main()
> {
>
>  long int N;
>  long long NN;
>
>  N  = (long int)  pow(2.0, 31.0);              // 2**31 = 
> 2*1024*1024*1024 = 2G
>  NN = (long long) pow(2.0, 32.0);              // 2**32 = 4G == 
> dreaded 32 bit boundary
>
>    std::cout << "long int N = " << std::hex << N << "\n"
>                  << "long long NN = " << std::hex << NN << std::endl;
>    return 0;
> }
>
> In the above, any leading zeroes will be suppressed.
>
> Another note that <cmath> is C++, and <math.h> is C.
> I don't recall off the top of my head if the second std::hex is 
> needed. Some manipulators work only for the next conversion, some work 
> until changed.
>
Additionally, to make it look more like a hex:
#include <iomanip> // for setfill
std::cout << "long int N = 0x" << std::hex <<  std::setw(16) << 
std::setfill('0') << N << "\n"
                 << "long long NN = 0x" << std::setw(16) << NN << 
std::endl;

-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id: 537C5846
PGP Key fingerprint: 3D1B 8377 A3C0 A5F2 ECBB  CA3B 4607 4319 537C 5846


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 251 bytes
Desc: OpenPGP digital signature
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20090218/f81a664b/attachment.bin 


More information about the gnhlug-discuss mailing list