Reputation: 1
I am storing a hexadecimal value in a uint64_t variable. When I call strtoull()
to convert the string to a hexadecimal value I get the correct result, but when I do a left-shift (<<
) by 32 I am losing the first two digits.
The string I am testing is "398a59b412"
. I expect to get back 398a59b41200000000
but instead of this I get 8a59b41200000000
.
#include <openssl/aes.h>
#include <openssl/modes.h>
#include <openssl/rand.h>
#include <openssl/hmac.h>
#include <openssl/buffer.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#define u8 unsigned char
#define INT_BITS 32
unsigned long long int ecountGeneration()
{
char *test_count = "398a59b412";
unsigned long long x = 0x398a59b412 | 0x00;
const char *test_bearer = "15";
const char *test_direction = "1";
unsigned long long result;
int64_t result_count = 0;
//errno = 0;
result_count = strtoull(test_count, '\0', 16);
uint64_t result_bearer = strtoull(test_bearer, NULL, 16);
uint64_t result_direction = strtoull(test_direction, NULL, 16);
result_count = result_count | 0x0;
uint64_t x1 = result_count<<32;
printf("-->%" PRIx64 "\n" ,x1);
uint64_t x2 = result_bearer<<27;
uint64_t x3 = result_direction<<26;
uint64_t x4 = x1 | x2 | x3;
printf("----->%" PRIu64 "\n" , x1);
return result;
}
Upvotes: 0
Views: 124
Reputation: 987
Because 0x398a59b412 is 40 bits in size!
In hexadecimal, each digit can have a range of 0
thru 15
, which requires 4 bits (aka a 'nibble'). 0x398a59b412
has 10 digits, so it is 40 bits size. Further, the <<
operator in C is not a 'cyclic shift', so the leftmost two nibbles are truncated (lost).
Upvotes: 1