How declare ?

Hi, I´m new in C and i´m trying to do this XOR;

2 hexadecimal numbers like:

0x CC 51 F2 75 13 E8 5C EB xor 0x 40 95 8E 2C 54 07 FA FF

(0x FF FF FF FF FF FF FF FF = 18446744073709551615 in decimal.)

But how declare in C these numbers ?

[ ] ´s
Last edited on
That's 16 hexadecimal characters, and each hex character is 4 bits. So you need a data type that can represent 4 * 16 = 64 bits.

That can be a long long int or an int64_t (if you can use standard types - #include <stdint.h>).

You will also need to specify that the hex number is a long long type by putting LL at the end of it.

Here's an example:
In order to represent 0x0123456789ABCDEF, you can do the following:

long long int number = 0x0123456789ABCDEFLL;
Last edited on
Hi,
Take a look in my code;

// 0xCC51F27513E85CEB xor 0x40958E2C5407FAFF //
// XOR = 8CC47C5947EFA614 //
#include <stdio.h>
#include <stdint.h>

long long int n_1 = 0xCC51F27513E85CEBLL;
long long int n_2 = 0x40958E2C5407FAFFLL;
long long int n_3;

main( ) {

n_3 = n_1^n_2;

printf("XOR = %x/n",n_3 );

return 0;
}

When i run the ans is not correct. XOR = 47EFA614. The correct is 8CC47C5947EFA614
You got the hexadecimal part right - the %x prints the number in hex.

However, do you see how the 47EFA614 is the lower 8 hexadecimal digits of the correct answer?

That's because the %x arguments means to print an unsigned integer, which is generally 32 bits (depending on your platform). So 32 bits is 8 hex characters.

Instead, use the %llx format, which tells it to use the long long format. That should give you your expected answer.
Thanks, worked !

[ ]´s
Topic archived. No new replies allowed.