Retrieving 64-Bit Integers

Hello,

I have been working on this for hours, but I cannot figure out what I am doing wrong. I am trying to convert a 64-bit integer to a character array and write it to a file. Here is the code I've been playing around with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <fstream>
#include <iostream>
#include <stdint.h>

int main (void) {

	uint64_t in_num = 9223372036854775807ULL;
	
	char * buffer = reinterpret_cast<char*>(&in_num);
	
	for (int a = 0; a < 8; a++) {
		std::cout << (int)buffer[a] << "\t";
	}
	std::cout << std::endl;
	
	uint64_t out_num = 0ULL;
	
	out_num = (
		(buffer[0] <<  0) | (buffer[1] <<  8) |
		(buffer[2] << 16) | (buffer[3] << 24) |
		(buffer[4] << 32) | (buffer[5] << 40) |
		(buffer[6] << 48) | (buffer[7] << 56)
	);
	
	std::cout << out_num;
}


I get these warnings, but it confuses me since I am working with 64-bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
main2.cpp: In function 'int main()':
main2.cpp:21:17: warning: left shift count >= width of type [enabled by default]

   (buffer[4] << 32) | (buffer[5] << 40) |
                 ^
main2.cpp:21:37: warning: left shift count >= width of type [enabled by default]

   (buffer[4] << 32) | (buffer[5] << 40) |
                                     ^
main2.cpp:22:17: warning: left shift count >= width of type [enabled by default]

   (buffer[6] << 48) | (buffer[7] << 56)
                 ^
main2.cpp:22:37: warning: left shift count >= width of type [enabled by default]

   (buffer[6] << 48) | (buffer[7] << 56)
                                     ^


It outputs this:
1
2
-1      -1      -1      -1      -1      -1      -1      127
18446744073709551615


I am expecting this number: 9223372036854775807

I think I am doing something stupid, but I've been programming this for a lot of hours. Does anyone have any ideas?

Thanks,
MaxterTheTurtle

[Edit:] I am on a little endian machine with the mingw32-w64 compiler.
Last edited on
 
    unsigned char * buffer = reinterpret_cast<unsigned char*>(&in_num);


1
2
3
4
5
6
7
8
    out_num = (static_cast<uint64_t>(buffer[0]) <<  0) |
              (static_cast<uint64_t>(buffer[1]) <<  8) |
              (static_cast<uint64_t>(buffer[2]) << 16) |
              (static_cast<uint64_t>(buffer[3]) << 24) |
              (static_cast<uint64_t>(buffer[4]) << 32) |
              (static_cast<uint64_t>(buffer[5]) << 40) |
              (static_cast<uint64_t>(buffer[6]) << 48) |
              (static_cast<uint64_t>(buffer[7]) << 56) ;
Topic archived. No new replies allowed.