I came to a point in my C++ book where I have to start using bitwise operators, such as &, ^, and |. I have a hard time grasping the concept of exactly how do I manipulate bits, the following example supposedly "turns off the 6th bit", therefore inverting the case of the character.

Here is my code:

#include <iostream>
using namespace std;

int main()
{
    string line;
    char ch;
    
    for(int i = 0; i < 10; i++) 
    {
          ch = 'a' + i;
          cout << ch;
          
          ch = ch & 223;
          
          cout << ch << " ";
    }
    
    cout << "\n";
    getline(cin,line);
    cin.get();
    return 0;
}

Now, in the line ch = ch & 223; it basically says the char ch equals to ...? I don't understand how the ch & 223 affects the case of the character. I DO know that uppercase and lowercase characters are 32 bits apart, right? And I DO see that 223 is exactly 32 less than 255, but I don't really know how it all works.
Please help, and thank you :]

There is no subtraction in boolean algebra, but bitwise operations like AND, OR, XOR ...

If you look at the ascii table, there is a decimal 32 between lower and upper case. Looking at the workflow in your program, you first print the lower case which has a greater number in the ascii table. It means that you need to subtract to get the upper case. To do a binary subtraction, you can xor the two numbers but here in your case, the int value of the lower case letter is AND masked with the complementary of subtractor. 223 is the complementary of 32 in byte notation (char is 1 byte long)

e.g. at the first step of the loop,
ch=a which is ascii 97
(97)10 = (01100001)2 = a
AND
(223)10 = (11011111)2
---------------------------
65(10) = (01000001)2 = A

You can get the same result with A ^ B (XOR)
or with A & comp(B) as in here.

You may see the gate logic here.

Value 223 (base 10) is 11011111 (base 2). Note that the only 0 bit is the 6th bit (counting from the right, the low end). So, when this value is bitwise ANDed with a number, every bit of the original value, except the 6th, will retain its original value.

Remember 1 & 1 => 1
0 & 1 => 0
0 & 0 => 0

So, in this problem, the 6th bit of ch will always be set to 0, the other bits are unchanged. In this particular sample, lower case letters are changed to their uppercase equivalent. If you started ch as 'A', you would find that it does not change.

That 6th bit holds the value 2^6, or 32(base 10)

This help?

Val

Just to note. It isn't necessary to use binary to work out this example. ASCII goes from 0 to 127.
'a' is 97. If you add 31, you're back to 0. You could add 95 to get the same result. 223 is another 128 on from that.

Thank you for both of your responses, I'm just trying to wrap my mind around the whole bit thing and how a character represents a binary number, and how the 6th bit reverses character case. So, I'm rereading your replies over and over trying to pound it into my head.

It would make more sense to use HEX values rather than DECIMAL. 223 is relatively meaningless, but 0xDF makes sense since each character is immediately translatable into binary:
D = 1101
F = 1111

223 would be considered a 'magic number' and should be avoided.

Ok, I think I have a rough concept of how this works, since AND and OR are used as a kind of way for subtracting and adding, does anyone know any links to where I could read up on bits and the whole memory managment thing more? thanks alot

Hello again, I have more questions, and I want to say I am very thankful for your help so far. What I'm wondering is that how come it matters which specific bit is turned on or off? And where would it be implemented in everyday use programs? When I say how come it matters which bit is turned on or off, I'm referring to the following example from my book:

... The AND operator is also useful when you want to determine wether a bit is on or off. For example, this statement checks to see if bit 4 in status is set: if(status & 8) cout << "bit 4 is on"; The reason 8 is used is that in binary it is represented as 0000 1000. That is, the number 8 translated into binary has only the fourth bit set. Therefore, the if statement can succeed only when bit 4 of status is also on. ...

What I'm wondering is does every decimal number have a specific use when using bitwise operators, was it just that specific 8, and again, where would the use of bitwise operators come in handy when programming in the C++ environment.

If anything is unclear please ask.

>>What I'm wondering is that how come it matters which specific bit is turned on or off? And where would it be implemented in everyday use programs?

Its often used to pack booleans in one integer. Suppose you are working with several led lights attached to a serial port and you need your program to know if a specific light is on or off. One way would be to define a bool flag for each light. But a more efficient way would be to bitmap and integer. The program could use the '&' operator with a mask to check if the light is on or off.

If you want to know how bitwise operators works see the Digital Electronic sites ,and look for Boelan Algebra(This is the Part of Digital Knowlage) .

Thanks a lot everyone, it really helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.