bitwise operations on numbers

Trying to write 4 bytes ints in a binary file and extract them after... I'm using the exclusive or (^) to isolate single bytes to write to and extract from the file since the write() function accepts only chars, only the beginning and end results are not the same... what am I doing wrong?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <ctime>
#include <fstream>
#include <cstdlib>

using namespace std;

const unsigned short NUMBER_OF_BYTES_IN_UNSIGNED_INT = 4;
const unsigned short NUMBER_OF_RANDOM_NUMBERS = 1000;

const unsigned int MAX_RANDOM_NUMBER = 2000000;

int main(int argc, char** argv)
{
	fstream map_template("map_template.bin", ios::in | ios::out | ios::binary | ios::trunc);
	
	int total_number, partial_number[3], memblock_index = 0;
	
	unsigned short memblock_size = NUMBER_OF_RANDOM_NUMBERS*NUMBER_OF_BYTES_IN_UNSIGNED_INT;
	
	char* memblock = new char[memblock_size];
	
	int rand_numbers[NUMBER_OF_RANDOM_NUMBERS];
	
	srand(static_cast<unsigned int>(time(NULL)));
	rand();
	
	for(unsigned short i = 0; i < NUMBER_OF_RANDOM_NUMBERS; i++)
	{
		rand_numbers[i] = rand()*MAX_RANDOM_NUMBER % MAX_RANDOM_NUMBER;
	}
	
	for(unsigned short i = 0; i < NUMBER_OF_RANDOM_NUMBERS; i++)
	{
		total_number = rand_numbers[i];
		
		partial_number[0] = total_number >> 24;
		partial_number[1] = total_number >> 16;
		partial_number[2] = total_number >> 8;
		
		memblock[memblock_index] = partial_number[0];
		memblock_index++;
			
		memblock[memblock_index] = partial_number[1] ^ (partial_number[0] << 8);
		memblock_index++;
		
		memblock[memblock_index] = partial_number[2] ^ (partial_number[1] << 8);
		memblock_index++;
		
		memblock[memblock_index] = total_number ^ (partial_number[2] << 8);
		memblock_index++;
	}
	
	if(map_template.is_open())
	{
		cout << "file opened...";
		
		map_template.seekp(0);
		map_template.write(memblock, memblock_size);
		
		map_template.seekg(0);
		map_template.read(memblock, memblock_size);
		
		map_template.close();
		
		cout << "done" << endl << endl;
	}
	else
	{
		cout << "file is not opened, exit" << endl << endl;
		return 0;
	}
	
	int results[NUMBER_OF_RANDOM_NUMBERS];
	
	memblock_index = 0;
	
	for(unsigned short i = 0; i < NUMBER_OF_RANDOM_NUMBERS; i++)
	{
		total_number = memblock[memblock_index] << 24;
		memblock_index++;
		
		total_number ^= memblock[memblock_index] << 16;
		memblock_index++;
		
		total_number ^= memblock[memblock_index] << 8;
		memblock_index++;
		
		total_number ^= memblock[memblock_index];
		memblock_index++;
		
		results[i] = total_number;
	}
	
	fstream log("log.txt", ios::out | ios::trunc);
	
	log << "input/output test -- 1st try" << endl << endl;
	
	for(unsigned short i = 0; i < NUMBER_OF_RANDOM_NUMBERS; i++)
	{
		log << rand_numbers[i] << " - " << results[i] << " - ";
		
		if(rand_numbers[i] == results[i]) log << "OK";
		else log << "*** *** *** not OK *** *** ***";
		
		log << endl;
	}

	system("pause");
	
	return 0;
}


Any ideas?

Thanks,
AeonFlux1212
Last edited on
I'm using the exclusive or (^) to isolate single bytes to write to and extract from the file
xor is not for isolating bits/bytes. It is for encryptin/decryption.
For isolation you need the bitwise and operator (&) [EDIT] bitwise and is &
Last edited on
I've implemented my own method to store the data, so it's working now, but I still don't understand why it's not working with << and >>... anyway the whole code is there... try it if you want ! Or how would you do it if you had to store ints in chars in a file...? :-)
The point is that xor encrypts data. You need to do the exact the same thing in reverse to decrypt it again. If you don't want encryption/decryption xor is useless and you can simply omit it.

store ints in chars in a file...?
If there is no good reason to do it otherwise: simply convert them from/to string. [f]stream is doing it for you with their overloaded << >> operator. Note: the stream operators are not binary shift operators.
Write:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	
	if(map_template.is_open())
	{
		cout << "file opened...";
		
		for(unsigned short i = 0; i < NUMBER_OF_RANDOM_NUMBERS; i++)
		{
			map_template << rand_numbers[i] << ' '; // Note: ' ' to separate numbers
		}
		
		map_template.close();
		
		cout << "done" << endl << endl;
	}
	else
	{
		cout << "file is not opened, exit" << endl << endl;
		return 0;
	}
Read:
1
2
3
4
5
6
7
8
9
	fstream ifs(...); // Note
	int results[NUMBER_OF_RANDOM_NUMBERS];
	
	index = 0;
	
	while(ifs >> results[index])
	{
		++index;
	}
Topic archived. No new replies allowed.