64-bit problem with Bitoperation

P

Patrick

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!
 
R

redfloyd

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)
 
P

Patrick

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
 
V

Victor Bazarov

Patrick said:
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
 
R

redfloyd

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!!!
 
J

James Kanze

On Aug 2, 5:25 pm, (e-mail address removed) 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.
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top