# c++ bitwise AND, expressing number literal

• 05-10-2010
rodrigorules
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' ?
• 05-10-2010
tabstop
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).
• 05-11-2010
iMalc
Quote:

Originally Posted by rodrigorules
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?
• 05-11-2010
prog-bman
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
• 05-11-2010
iMalc
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)`