# bitwise operators?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-02-2006
EvilPickles
bitwise operators?
Ok I've gotten so far intomy book, that it is discussing bitwise operators. It has explained how the And works, I understand that now (although it took some time).

What I do not get is how this program works:

Code:

```#include <iostream> int main(); { char ch; for(int i = 0 ; i < 10; i++) { ch = 'a' + i; /*increment the unicode (I think that is the name, I forget what the character datanabase is called) for the character, causing a to become b, and so on*/ cout << ch; ch = ch & 223; /* the book says that this operation turns off the sixth bit, which somehow makes the bit number representing the lower case charcaters increment by 32, to reach the uppercase area of the unicode database*/ } cout << '\n'; return 0; }```
I do not understand how using the and symbol with the charcter increases unicode symbol by 32. I do not know how to count in bits/binary/whatever, but I assume that the book which is describing that the sixth bit causes the change to uppercase by changing the sixth bit, which must have somekind of effect on the math. Also when it mentions the sixth bit, is that from left to right, or right to left?
• 08-02-2006
twomers
Ok, I think there's a tutorial about it somewhere, but the & symbol (as well as doing other things), is the bitwise AND operator. Essentially if you had the following two bytes:

Code:

```00010011 00000111```
and you &'d them together, the resultand byte is attained by AND-ing together all the individual bytes (starting from either end, I'll start on right)

Code:

```1 AND 1 = 1 // 0th bits 1 AND 1 = 1 // 1st bits 0 AND 1 = 0 // 2nd bits 0 AND 0 = 0 // etc 1 AND 0 = 0 etc.```
so the byte you get back, is -

Code:

`00000011`
get it. This can be used to increase numbers as your code does.
• 08-02-2006
itsme86
bit 1 = 1 decimal
bit 2 = 2 decimal
bit 3 = 4 decimal
bit 4 = 8 decimal
bit 5 = 16 decimal
bit 6 = 32 decimal
...

See it now?
• 08-02-2006
Richie T
You need to read up on the binary number system to be able to understand this.

As a programmer, you should also look at this - it's ASCII in question here, not
Unicode - that's a different creature altogether!

The binary representation of decimal 223 is 11011111

Using bitwise and on this number and some other value, you find that the sixth
bit of the other value is always set to zero, and if you read the article on binary
above, you will eventually see why this action decreases the value by 32.

Also, your code has two issues:

int main(); { // should not have a semicolon here

and you should also have a "using namespace std" above your main.
• 08-02-2006
twomers
and the whole ultra commenting thing eh? :D
• 08-02-2006
Mario F.
Quote:

Originally Posted by Richie T
and you should also have a "using namespace std" above your main.

err... ;)
• 08-02-2006
Richie T
Quote:

Originally Posted by Mario F.
err... ;)

Oops! correct one syntax error, introduce another! Also, just so we're clear, the quotes
aren't necessary when putting it in code!
• 08-02-2006
Mario F.
No, I meant it would be preferable to not introduce the whole namespace. Qualifying each name or doing it with using declarations is safer.

std::cout, is my favorite

using std::cout, is the lesser evil

using namespace std, really defeats the purpose of namespaces.
• 08-02-2006
EvilPickles
Twomers 1st post, I didn't understand that for a while, but I mention that I figured that part out in the end, you did not have to explain that out (Althoug you did explain it differently and more clearly than the book did).

Quote:

Posted by: itsme86:
bit 1 = 1 decimal
bit 2 = 2 decimal
bit 3 = 4 decimal
bit 4 = 8 decimal
bit 5 = 16 decimal
bit 6 = 32 decimal
...

See it now?
No, I don't in fact, Mr. Perfect. Not everyone knows everything you do, there is no need to be rude and egotistical.

Quote:

Posted by: richie T:
You need to read up on the binary number system to be able to understand this.

As a programmer, you should also look at this - it's ASCII in question here, not
Unicode - that's a different creature altogether!

The binary representation of decimal 223 is 11011111

Using bitwise and on this number and some other value, you find that the sixth
bit of the other value is always set to zero, and if you read the article on binary
above, you will eventually see why this action decreases the value by 32.

Also, your code has two issues:

int main(); { // should not have a semicolon here

and you should also have a "using namespace std" above your main.
Thanks for the link. Also any mistakes in this code do not matter, I copied them from the book and made sure it was at least logical. It did explain that the decimal value of 223 is 1101 1111, although it placed a space between the two four letter groupings.

As for unicode, I assumed this was unicode, my first language Visual Basic .net, used unicode, I assumed all languages did, although that was an incorrect assumption. Thank you for your time, and help. If this seems a bit emotionless and drawling that is because I am in a bad mood, I am having 'people problems'.
• 08-02-2006
swoopy
>I do not understand how using the and symbol with the charcter increases unicode symbol by 32.
Good thing, because actually it subtracts 32, assuming the sixth bit is a one:
Code:

```#include <iostream> using namespace std; int main() { char ch; for(int i = 0 ; i < 10; i++) {   ch = 'a' + i; /*increment the unicode (I think that is the name, I forget what the character datanabase is called) for the character, causing a to become b, and so on*/   cout << ch << " (" << (int) ch << ")" << "  ";   ch = ch & 223; /* the book says that this operation turns off the sixth bit, which somehow makes the bit number representing the lower case charcaters increment by 32, to reach the uppercase area of the unicode database*/   cout << ch << " (" << (int) ch << ")" << endl; } return 0; }```
Code:

`  ch = ch & 223;`
is the same as:
Code:

`  ch = ch & 0xdf;  //11011111`
So the code clears the 6th bit (I normally call that bit 5 starting from bit 0).

>Also when it mentions the sixth bit, is that from left to right, or right to left?
Right to left, or least significant bit (lsb) to most significant bit (msb).
• 08-02-2006
quzah
Quote:

Originally Posted by EvilPickles
No, I don't in fact, Mr. Perfect. Not everyone knows everything you do, there is no need to be rude and egotistical.

Well you're a ........ing idiot then. They weren't being rude and egotistical. They implied this question:

"Do you see a pattern here?"
1 - 1
2 - 2
3 - 4
4 - 8
5 - 16
6 - 32
7 - ?

If you cannot fill in what 7 will be, then you're a ........ing idiot. That rude enough for you? Idiot.

Since I'm here, there's also a FAQ on all this, if you had taken two seconds to find it. Oh, and there's also a SEARCH BUTTON that would have given you your answer too. So not only are you an idiot, you're a lazy ........ing idiot.

Quzah.
• 08-02-2006
Loctan
Quote:

Originally Posted by EvilPickles
Twomers 1st post, I didn't understand that for a while, but I mention that I figured that part out in the end, you did not have to explain that out (Althoug you did explain it differently and more clearly than the book did).

No, I don't in fact, Mr. Perfect. Not everyone knows everything you do, there is no need to be rude and egotistical.

Look at it this way. In decimal a value:

XYZ

For Z: Z can equal a 0 ... 9 meaning you it adds either a 0,1,..., or a 9 to the value
For Y: Y can equal a 0 ... 9 meaning you it adds either a 0, 10, 20, ..., or a 90 to the value
For X: X can equal a 0 ... 9 meaning you it adds either a 0, 100, 200, ..., or a 900 to the value

The same is true for binary:

XYZ

Z is bit position 0
Y is bit position 1
X is bit position 2

For Z: Z can be a 0 or a 1 and adds Z*(2^0) or Z*1 to the value. So if Z is a 1 it adds 1, if it's a 0 it adds 0

For Y: Y can be a 0 or a 1 and adds Y*(2^1) or Y*2 to the value. So if Z is a 1 it adds 2, if it's a 0 it adds 0

For X: X can be a 0 or a 1 and adds X*(2^2) or X*4 to the value. So if X is a 1 it adds 4, if it's a 0 it adds 0

Code:

```Binary        Decimal 000 = 0*(2^2) + 0*(2^1) +0*(2^0) = 0 001 = 0*(2^2) + 0*(2^1) +1*(2^0) = 1 010 = 0*(2^2) + 1*(2^1) +0*(2^0) = 2 011 = 0*(2^2) + 1*(2^1) +1*(2^0) = 3 100 = 1*(2^2) + 0*(2^1) +0*(2^0) = 4 101 = 1*(2^2) + 0*(2^1) +1*(2^0) = 5 110 = 1*(2^2) + 1*(2^1) +0*(2^0) = 6 111 = 1*(2^2) + 1*(2^1) +1*(2^0) = 7```
This is what he was alluding to. I hope it makes sense.
• 08-02-2006
Richie T
Quote:

Originally Posted by Mario F.
No, I meant it would be preferable to not introduce the whole namespace. Qualifying each name or doing it with using declarations is safer.

std::cout, is my favorite

using std::cout, is the lesser evil

using namespace std, really defeats the purpose of namespaces.

Be that as it may, I don't consider it an issue for trivial programs. I find std::cout best too,
as you only use what you need, when you need it, but my posts here typically use the entire
namespace out of habit.

>>Thank you for your time, and help.

You're welcome!

>>If this seems a bit emotionless and drawling that is because I am in a bad mood, I am having
'people problems'.

That's fine, but what you said to itsme86 was harsh. On this board we generally like to supply
just enough info for a person to figure/find out the answer, that's all he was doing there, not
trying to insult you.

As for quzah's remarks, he's basically saying what I just said above. He has a low tolerance for
"rudeness", I warn you not to rise to him because you will not win in an argument with him because
he really knows his stuff, and will leave you reeling!
• 08-02-2006
EvilPickles
I apologize to itsme86, I reacted the way I did because the moment I joined a server in the game I play, I got hounded by the entire server (a large grpup of idiots), about a post I once made here. I used to brag you could google my name and come up with everything I've ever written, that was true until other people started using this name, either intentionally or not. A search for the site which the game belongs to is what these people searched for. I looked it up on accident, and the forum post with that turned up the search result was not a thread I had ever posted in nor did I find the post anywhere within the thread (Ctrl + f didn't find anything!).

The two main agitators acted like I said itsme86 had acted, like Mr. perfect. I've seen the effect before in which an argument occurs, one person says something who has not said anything before, and turns the entire server (most people in it) against the person who would be 'winning' the argument. I understand that arguing online is very pointless and that even if you win, no one will care.

I can barely comprehend some of the text you linked in that article, I find it difficult to read something if I do not understand what they are saying. The article uses a very complex vocabulary system, and wikipedia's nature of linking to other articles annoys me. If I wanted to I could read all that, but I get very bored trying to comprehend it all. I shall check that out again later, if later ever comes. -.-
• 08-02-2006
Mario F.
Wow... a soap.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last