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

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    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. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    I have absolutely no idea why you would want to do this. Are you trying to turn "abc" into 0x00000003? What for?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    It's for a lab.

    And no... a b c would equal

    00000000000000000000000000000111

    or... the integer 7.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    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. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM

Tags for this Thread