64-bit problem with Bitoperation



 DEVELOP > c-Plus-Plus > 64-bit problem with Bitoperation

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Patrick"
Date: 02 Aug 2007 10:13:24 AM
Object: 64-bit problem with Bitoperation
Hi
I wanna do a simple xor of two 64 bit variables as in the following code
fragment:
unsigned long long test0 = 0x8888000000001210LL;
unsigned long long test1 = 0x88111111111100EFLL;
unsigned long long test2;
test2 = test0 | test1;
printf("0x%016x ",test2);
Unfortuantely only the lower 32 bits of the result are correct as the
output shows
0x00000000111112ff
Anyone an idea why the higher 32 bits are not affected by the bitoperation?
many thanks!
.

User: ""

Title: Re: 64-bit problem with Bitoperation 02 Aug 2007 10:25:56 AM
On Aug 2, 8:13 am, Patrick <sportfre...@gmx.at> wrote:

Hi

I wanna do a simple xor of two 64 bit variables as in the following code
fragment:

unsigned long long test0 = 0x8888000000001210LL;
unsigned long long test1 = 0x88111111111100EFLL;
unsigned long long test2;

test2 = test0 | test1;

printf("0x%016x ",test2);

Unfortuantely only the lower 32 bits of the result are correct as the
output shows

0x00000000111112ff

Anyone an idea why the higher 32 bits are not affected by the bitoperation?

Since long long is not supported by the Standard, you're probably
better off in a newsgroup dedicated
to your compiler (I'm guessing gcc, so try gnu.g++.help).
That said, my guess is that your printf format is incorrect. Try
%0x016llx (note that's 0-one-6-ell-ell-x)
.
User: "Patrick"

Title: Re: 64-bit problem with Bitoperation 02 Aug 2007 10:40:46 AM

That said, my guess is that your printf format is incorrect. Try
%0x016llx (note that's 0-one-6-ell-ell-x)

Thanks I posted it to the compiler newsgroup. The used your format and
now the output is completly weird.
ffffffff016llx as result with the following code fragment:
unsigned long long test0 = 0xEEEEEEEEEEEEEEEELL;
unsigned long long test1 = 0x1111111111111111LL;
unsigned long long test2;
test2 = test0 ^ test1;
printf("%0x016llx",test2);
cheers,
Patrick
.
User: "Jorgen Grahn"

Title: Re: 64-bit problem with Bitoperation 03 Aug 2007 05:36:26 AM
On Thu, 02 Aug 2007 16:40:46 +0100, Patrick <sportfreund@gmx.at> wrote:


That said, my guess is that your printf format is incorrect. Try
%0x016llx (note that's 0-one-6-ell-ell-x)


Thanks I posted it to the compiler newsgroup. The used your format and
now the output is completly weird.

ffffffff016llx as result with the following code fragment:

unsigned long long test0 = 0xEEEEEEEEEEEEEEEELL;
unsigned long long test1 = 0x1111111111111111LL;
unsigned long long test2;

test2 = test0 ^ test1;

printf("%0x016llx",test2);

Whenever possible, enable all warnings from your compiler. GCC knows
about printf format strings:
tuva:/tmp> g++ -Wall -W -pedantic -Wno-long-long -std=c++98 -c foo.cc
foo.cc: In function 'void f()':
foo.cc:10: warning: format '%0x' expects type 'unsigned int', but
argument 2 has type 'long long unsigned int'
I expect other compilers to have it too -- it's not that complicated
to implement, and it catches a whole class of bugs.
/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
.

User: "Victor Bazarov"

Title: Re: 64-bit problem with Bitoperation 02 Aug 2007 10:52:31 AM
Patrick wrote:

That said, my guess is that your printf format is incorrect. Try
%0x016llx (note that's 0-one-6-ell-ell-x)


Thanks I posted it to the compiler newsgroup. The used your format and
now the output is completly weird.

ffffffff016llx as result with the following code fragment:

unsigned long long test0 = 0xEEEEEEEEEEEEEEEELL;
unsigned long long test1 = 0x1111111111111111LL;
unsigned long long test2;

test2 = test0 ^ test1;

printf("%0x016llx",test2);

I believe you need to lose the first 0x:
printf("%016llx",test2);
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: ""

Title: Re: 64-bit problem with Bitoperation 02 Aug 2007 06:11:01 PM
On Aug 2, 8:52 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Patrick wrote:

That said, my guess is that your printf format is incorrect. Try
%0x016llx (note that's 0-one-6-ell-ell-x)


Thanks I posted it to the compiler newsgroup. The used your format and
now the output is completly weird.


ffffffff016llx as result with the following code fragment:


unsigned long long test0 = 0xEEEEEEEEEEEEEEEELL;
unsigned long long test1 = 0x1111111111111111LL;
unsigned long long test2;


test2 = test0 ^ test1;


printf("%0x016llx",test2);


I believe you need to lose the first 0x:

printf("%016llx",test2);

Thanks, Victor. It's been so bloody long since I've used printf(), I
didn't even realize the 0x was in the wrong place!!!
.



User: "James Kanze"

Title: Re: 64-bit problem with Bitoperation 03 Aug 2007 04:50:46 AM
On Aug 2, 5:25 pm,
wrote:
[...]

Since long long is not supported by the Standard, you're probably
better off in a newsgroup dedicated
to your compiler (I'm guessing gcc, so try gnu.g++.help).

That's only partially true. The current draft for the next
version of the standard includes long long, and the issues
surrounding it have been more or less stable for some time now.
I would expect that any C++ compiler which supports long long do
so in a fashion compatible with this draft.

That said, my guess is that your printf format is incorrect. Try
%0x016llx (note that's 0-one-6-ell-ell-x)

Just another reason to avoid printf.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER