bitwise operators?

This is a discussion on bitwise operators? within the C++ Programming forums, part of the General Programming Boards category; Ok I've gotten so far intomy book, that it is discussing bitwise operators. It has explained how the And works, ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    14

    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?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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?
    If you understand what you're doing, you're not learning anything.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    and the whole ultra commenting thing eh?

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Quote Originally Posted by Richie T
    and you should also have a "using namespace std" above your main.
    err...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    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).

    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.

    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'.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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).
    Last edited by swoopy; 08-02-2006 at 04:56 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  12. #12
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    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.

  13. #13
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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!
    Last edited by Richie T; 08-02-2006 at 05:17 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    14
    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. -.-

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Wow... a soap.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 01:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM

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