Using bitmask operators

So this is the code I have so far which puts a bitmask seperator base on what I choose. My problem is when Im trying to do them in this manner.

1. 0 to 31, with minimum width set to 8, and separating between every 4 digits.
2. 2^0 to 2^16, with minimum width set to 17, separating between every 8 digits.
3. (2^0-1) to (2^16-1) with minimum width set to 16, and no seperation between digits.

I try doing number 2 called "String multyplyByTwo" but seems to be getting errors.
If anyone can point me to the right direction would greatly appreciate it.





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
#include<iostream>
#include<string>

using namespace std;

string binToChar( const int n, unsigned minWidth = 8, unsigned sepMask = 0x11111110 )

{

string binToChar = "";
unsigned mask = 0x80000000;
minWidth = 32 - minWidth; 



while ( minWidth-- && ( n & mask ) == 0 ) 
	{
		mask >>= 1;
	}

	do
	{
		if( n & mask)
		{
			binToChar = binToChar + "1";
		}
		else
		{
			binToChar = binToChar + "0";
		}
		if ( mask & sepMask )
		{
			binToChar = binToChar + "_";
		}
		mask = mask >> 1;
		
	}
	while( mask );

	return binToChar;
}	


int main( int argc, char* argv[] )
{
	

cout << dec << 0x68 << "\t" << hex << 0x68 << "\t" << binToChar(0x68, 8, 0xB8 ) << endl;

cout << dec << 0x7B << "\t" << hex << 0x7B << "\t" << binToChar(0x7B, 8, 0x1D ) << endl;
cout << dec << 0x3C << "\t" << hex << 0x3C << "\t" << binToChar(0x3C, 8, 0XA ) << endl;
	cout << "\n" << endl;

	for ( int x = 0; x < 32; x++)
	{
		cout << dec << x << "\t" << hex << x << "\t" << binToChar(x)

<< "\t" << endl;
	}

	String multyplyByTwo ( const int number )
	
	int number = 2;
	
	{
		for ( int number = 0; number < 32; number++);

		cout << dec << number << hex << number << binToChar(number) << endl;
	}
}


The binToChar function looks like it should be working.. except I have no idea what the sepMask parameter is supposed to do. It looks like it just inserts underscores throughout the string?


Can you give a more clear example of what you're doing? Please advise the below:

1) An example function call: (ie: binToChar(0x11, 8, 0xF3))

2) Your desired output (what you'd want the function to return with that call)

3) The actual output (what your function is actually returning).



With those 3 things it'll be much easier to understand and spot the problem.
First of all thank you for replying. The binToChar does work its the multyByTwo function that I cant seem to get right. I'm doing three different calls for the three questions so 3 seperate function calls and the sepMask does put an underscore based on the desire number, for example if i choose questions one the output should be " 0000_0000" for #2 "0_00000000_00000001 and for #3
"00000000000000000"
String multyplyByTwo ( const int number )

is a function declaration. And the likely error code (which you should've supplied) probably mentions a missing semi-colon. It sort of looks like you're trying to define the function inside of main, and you cannot do that.

As an aside, it's spelled multiply.
Topic archived. No new replies allowed.