c++ bitwise AND, expressing number literal

This is a discussion on c++ bitwise AND, expressing number literal within the C++ Programming forums, part of the General Programming Boards category; lets say I have a string of characters, (I am sure they are all letters) I want to make an ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    c++ bitwise AND, expressing number literal

    lets say I have a string of characters, (I am sure they are all letters)

    I want to make an is_uppercase function, thus my goal in this exercise is to check if the 5 bit is a 0 or not... (adding 32 to any uppercase gives me a lowercase)

    Code:
    bool is_uppercase(std::string word)
    {
    	BOOST_FOREACH(char c, word)
    	{
    		if(c & 32)
    		{
    			return false;
    		}
    	}
    	return true;
    }
    The above code seems to work, though in this case I do not have to do any operations on the number.

    but If i needed to use the ~ operator on the number 32 it would have messed me up by making the number negative...

    Code:
     
    if ( ~c | ~32)
    // yields incorrect result
    ive tried
    Code:
     ~(32u)
    but it also leads me to -33 .. (which is wrong)

    how would I express a number as 'unsigned' ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm not sure what your question is, exactly. ~32 is bitwise flip, so instead of
    00010000
    you have
    11101111. Or'ing that with ~c yields a number with 1's where either c or 32 had a zero, or to put it another way it gives ~(c & 32).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Quote Originally Posted by rodrigorules View Post
    but If i needed to use the ~ operator on the number 32 it would have messed me up by making the number negative...

    Code:
    if ( ~c | ~32)
    // yields incorrect result
    ive tried
    Code:
     ~(32u)
    but it also leads me to -33 .. (which is wrong)
    No it doesn't. ~32u is the correct way to represent an unsigned number with the desired bits set.

    if ( ~c | ~32) is the same as writing if (true) because or-ing a number with another number can only set additional bits, so you're guaranteed to get a non-zero result from the or-expression and hence a true condition overall.
    What were you actually wanting that to do?
    Last edited by iMalc; 05-11-2010 at 01:25 AM.
    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"

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Is the goal to learn how to check if a character is upper case or just to write a function to see if a string is all upper case.

    If its the latter you can just use this:
    isupper - C++ Reference
    Woop?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    If you were trying to check for the other case then all you want to do is to negate the expression you had. Thus you could do:
    Code:
    if (!(c & 32))
    Bitwise negating c would also work:
    Code:
    if (~c & 32)
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21