Thread: Bit manipulation of unsigned long long

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    48

    [SOLVED] Bit manipulation of unsigned long long

    I am using an unsigned long long to do bit manipulation. When trying to tell if bits are set or not, my code detects that bit 32 is set even when it is not. Here is my code, the if statement is where the problem is.

    Code:
    void recover(unsigned int branch_id)
    {
    	unsigned long long mask = gbm.get_younger_branches(branch_id);
    
            /* mask prints as: 0x7 here */
            printf("0x%llX\n", mask);
    
    	for (int i = 0; i < entries.size(); ++i)
    	{
    		for (int j = 0; j < sizeof(unsigned long long) * 8; ++j)
    		{
                      
                            /* this gets entered for j = 0, 1, 2, 32 */
    			if (mask & (1 << j))
    			{
    				entries[i]->release_branch_id(j);
    			}
    		}
    	}
    }
    Thanks.
    Last edited by BdON003; 10-08-2011 at 05:32 PM.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    I solved it.

    Code:
    void recover(unsigned int branch_id)
    {
    	unsigned long long mask = gbm.get_younger_branches(branch_id);
    
            /* mask prints as: 0x7 here */
            printf("0x%llX\n", mask);
    
    	for (int i = 0; i < entries.size(); ++i)
    	{
    		for (int j = 0; j < sizeof(unsigned long long) * 8; ++j)
    		{
                      
                            /* this gets entered for j = 0, 1, 2, 32 */
    			if (mask & (static_cast<unsigned long long>(1) << j))
    			{
    				entries[i]->release_branch_id(j);
    			}
    		}
    	}
    }

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You "solved" it huh...
    There's the cleaner equivalent of exactly how you were doing it:
    Code:
                if (mask & (1ULL << j))
    And then there's the smarter way to do what you wanted which doesn't involve 64-bit constants at all:
    Code:
                if ((mask >> j) & 1)
    Don't mark your threads as solved.
    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. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  2. unsigned long long division print
    By Kempelen in forum C Programming
    Replies: 4
    Last Post: 01-30-2009, 10:03 AM
  3. Should I cast to long or unsigned long?
    By cpjust in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2008, 02:04 AM
  4. Unsigned long long to something longer
    By JFonseka in forum C Programming
    Replies: 18
    Last Post: 05-26-2008, 07:34 AM
  5. unsigned long long to string conversion
    By Wiretron in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2007, 04:02 AM