# Thread: Using the bits of an unsigned int as letter sets. HELP PLEASE

1. ## Using the bits of an unsigned int as letter sets. HELP PLEASE

I am trying to use an unsigned int and its respective bits to assign a set of lowercase letters. The low order bit represents the letter 'a' and the next higher order bit represents the letter 'b,' all the way to z.

For example... 3 in 32-bit binary is... and represents the set { a, b }

00000000000000000000000000000011

The number 41 is... { a, d, f }

00000000000000000000000000101001

How would I go about reading the bit and outputting the letters in the set?

Using something like:
﻿
Code:
```int main()
{
unsigned int n1, n2;

cout << "Please insert your first number used for calculation:" << endl;
cin >> n1;
cout << "Please insert your second number now:" << endl;
cin >> n2;

char ch = getChar(n1);
}```

2. I have absolutely no idea why you would want to do this. Are you trying to turn "abc" into 0x00000003? What for?

3. It's for a lab.

And no... a b c would equal

00000000000000000000000000000111

or... the integer 7.

4. So basically it's bitflags.
If one were doing the reverse, rather than having the user input a number for which bit to set, the user enters a letter for each bit to set. Perfectly logical.
Here's a few hints for how to do the reverse, see how far that gets you:
'a' - 'a' == 0
'd' - 'a' == 4
1 << 0 == 00000000000000000000000000000001 base2
1 << 4 == 00000000000000000000000000001000 base2

5. I think I got it.... There are a few other things I had to do also... Is there an easy way to condense this down using functions or whatever? I'm sure there is... but it's too late to try now and was just wondering.

Here is what I have:

#include <iostream>
using namespace std;

Code:
```int main()
{
unsigned int n1, n2, temp, temp2;

cout << "Please insert your first number used for calculation:" << endl;
cin >> n1;
cout << "Please insert your second number now:" << endl;
cin >> n2;

char ch = 'a';
char ch2 = 'a';

temp = n1;
temp2 = n2;

// Part "A" //
cout << endl << "Set A = { ";
for(int i = 0; i < 26; i++)
{
if(temp%2 == 1)
{
cout << ch << " ";
ch++;
temp = temp >> 1;
}
else
{
temp = temp >> 1;
ch++;
}
}
cout << "}" << endl;

cout << "Set B = { ";
for(int i = 0; i < 26; i++)
{
if(temp2%2 == 1)
{
cout << ch2 << " ";
ch2++;
temp2 = temp2 >> 1;
}
else
{
temp2 = temp2 >> 1;
ch2++;
}
}
cout << "}" << endl;

// Part "B" //
int Union = n1 | n2;
temp = Union;
ch = 'a';

cout << "Union of the 2 sets = { ";
for(int i = 0; i < 26; i++)
{
if(temp%2 == 1)
{
cout << ch << " ";
ch++;
temp = temp >> 1;
}
else
{
temp = temp >> 1;
ch++;
}
}
cout << "}" << endl;

// Part "C" //
int Intersection = n1 & n2;
temp = Intersection;
ch = 'a';

cout << "Intersection of the 2 sets = { ";
for(int i = 0; i < 26; i++)
{
if(temp%2 == 1)
{
cout << ch << " ";
ch++;
temp = temp >> 1;
}
else
{
temp = temp >> 1;
ch++;
}
}
cout << "}" << endl;

// Part "D" //
int Difference = Intersection ^ n1;
temp = Difference;
ch = 'a';

cout << "Difference of the 2 sets = { ";
for(int i = 0; i < 26; i++)
{
if(temp%2 == 1)
{
cout << ch << " ";
ch++;
temp = temp >> 1;
}
else
{
temp = temp >> 1;
ch++;
}
}
cout << "}" << endl << endl;

}```

6. Yes, you could have made it shorter by putting the for loop into its own function, considering it appears five times. Well done getting it sorted anyway.

Popular pages Recent additions