Thread: Bitwise Operators....

  1. #1
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138

    Bitwise Operators....

    I just finished the 2nd Chapter in Beginning Visual C++ 6 by Ivor Horton, and when doing the exercises found that I did not really understand how Bitwise Operators work, and why they are of any importance. So if anyone can give me a short, yet logical explanation or point me towards an online tutorial that covers this please reply. Thanks!

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >and why they are of any importance

    look up hash code generating using bit indexing. Bit wise operators become very usefull here.

    heres a short and maybe logical explanation of how they work

    Code:
    bit1 = 1001
    bit2 = 0101
    
    bit1 & bit2 = 1&0, 0&1, 0&0, 1&1
                     =  0        0      0       1
    
    bit1 | bit2 = 1|0, 0|1, 0|0, 1|1
                    =   1     1     0      1
    etc....
    you just perform the operations on the individual bits.

  3. #3
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278

    Re: Bitwise Operators....

    Originally posted by o0obruceleeo0o
    ...and why they are of any importance.
    They are used often for 'flags' in programs, in which a variable in a single byte must store multiple on/off switches (one for each bit).

    Also, if you wish to use modulus on an even computer number (2^n,n=integer), then it is the same as using a bitwise AND, if you use the number one less than the modulus. Example:
    n % 8 is equal to n & 7
    n % 32 is equal to n & 31

    There are tons of other reasons.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    I'm studying that book too. It's not as thorough as Ivor Horton's Beginning C++, but in some ways I think it might be better for beginners because they won't get bogged down in too much detail--it took me a long time to finish Beginning C++. With Beginning Visual C++, I skipped the first five chapters, and I'm reviewing classes and working my way towards the windows programming and MFC part.

    However, I remember the bitwise operator section from Beginning C++ because it almost caused me to abandon the book. It was so complicated, I was forced to skip the section, and since nothing built on it later, I was OK, and the book turned out to be great. In my opinion, bitwise operators should be in an appendix because it's a great way to turn a beginner away from the language. You may never need to use bitwise operators, so you shouldn't worry about them. If you understand the basic AND'ing and OR'ing of two numbers together, that's plenty.

    PM me through the bulletin board if you have any specific questions about examples or problems in the book.
    Last edited by 7stud; 04-07-2003 at 12:18 AM.

  5. #5
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by 7stud
    In my opinion, bitwise operators should be in an appendix because it's a great way to turn a beginner away from the language.
    It should be explained in the book, since you will come across bitwise operators, and you must understand them. So, it cannot be just an appendix. But, I agree that not enough examples or explanation of their use is supplied in the book.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Hmmm, I thought there was something in the tutorials or the FAQ... didn't see it just now.

    You can do a lot of programming without worrying about bits, binary, or hex. But, people who program embedded processors, program in assembly language, or work in the hardware world (like me) use binary, hex, and bitwise operators all the time. One bit might be used to turn on an LED or a relay, or to monitor the status of a switch. I'm often using an oscilloscope to check the state (or activity) of individual bits. My test programs often set, clear, or read individual bits.

    I don't have the Horton book, but if I was writing a "bitwise" chapter, I'd start with a single-bit boolean logic... 1 AND 1 = 1, 1 AND 0 = 0, 1 OR 0 = 1.. etc. Next I'd work on binary and hex number systems, then finaly bitwise operations on bytes, words, etc. So, you might want to study-up on boolian logic and binary and hex first. Hmmm... I might actually start my chapter with a tiny bit of digital circuitry, where 0 = 0 volts and 1 = 5 volts... to make it a bit less abstract (?). I learned about boolean logic and logic circuitry before I learned how to do it in software.

    This must seem very abstract and confusing to someone with no knowledge of digital circuits who is learning a high level language, unless you've already learned this stuff in a math class. But, it's really NOT complex.

    [EDIT]
    Here's some boolean logic info: www.cplusplus.com/doc/papers/boolean.html
    Last edited by DougDbug; 04-07-2003 at 12:47 PM.

  7. #7
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    Thanks for all the great replies! I understand that 1&0=0 etc.... but I guess my knowledge of binary is too limited to understand how that helps me much. On a side note I just started reading "Code" by Petzoid, and im on the binary chapter, so hopefully that will help me with all the binary stuff .

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I don't have the Horton book, but if I was writing a "bitwise" chapter, I'd start with a single-bit boolean logic... 1 AND 1 = 1, 1 AND 0 = 0, 1 OR 0 = 1.. etc. Next I'd work on binary and hex number systems, then finaly bitwise operations on bytes, words, etc. So, you might want to study-up on boolian logic and binary and hex first. Hmmm... I might actually start my chapter with a tiny bit of digital circuitry, where 0 = 0 volts and 1 = 5 volts... to make it a bit less abstract (?).
    The problem is that complex bitwise operations are being presented in chapter 2 of a beginning C++ book in which the basic variable types are introduced. I was able to figure out and understand 880 pages of the C++ language, but I never did figure out the complex section on bitwise operators. I don't think that's the way to order the material. However, if your goal is to slap down anyone trying to learn C++, that's the best way to do it. Anyone giving up at that point would have an impression that C++ is a complex bitwise manipulation language, which is ridiculous. In fact, in the rest of my book, I don't think bitwise operators were ever mentioned again.

    If you started your chapter with digital circuitry, you would cause 90% of the readers to give up. There are so many more important things to learn about C++, that bitwise operations should be reserved for the last chapter of a beginning text, if included at all. Personally, I think any mention of bitwise operators early on in a beginning text should start and end with basic AND'ing and OR'ing.
    Last edited by 7stud; 04-07-2003 at 02:39 PM.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Thumbs up RIGHT!

    7stud:
    You're absolutely right. I just mentioned the digital circuitry stuff because it helps me actually visualize the little ones and zeros in there. And, to give an example of what it's good for... because otherwise why in the heck would anyone want to use this stuff? For me, it makes it less abstract, but I suppose for someone who doesn't know what a volt is, it's even more abstact! This also would be very easy for someone who already understands the math / logic.

    o0obruceleeo0o:
    If you understand the AND, OR, NOT, and XOR stuff, then you're 90% there. I think what makes this difficult, is that C++ is not very "binary friendly." (You can't just stick binary numbers into you source code, etc.) You're probably getting bogged-down in the Binary to decimal (or hex) conversion. BTW - you shouldn't have to deal with number base conversion in the 2nd chapter either!

    Let's try this... Easy in binary!
    01010101
    00001111
    ------------ AND
    00000101

    01010101
    00001111
    -------------OR
    01011111

    In hex...
    55 h
    0F h
    --- AND
    05 h

    55 h
    0F h
    --- OR
    5F

    In decimal
    085
    015 *
    ---- AND
    005

    085
    015 *
    ---- OR
    095

    Examples:
    Testing / manipulating bit 1 (Starting with bit zero at the far left.)

    if(X & 2) // If bit 1 is high.. Note- 0010 binary = 2 decimal

    X = X & 2; // Force bit 1 high

    Darn! I must be in a wordy mood today!

    *[Corrected... oops!]
    Last edited by DougDbug; 04-07-2003 at 06:10 PM.

  10. #10
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Hmmm, I thought there was something in the tutorials or the FAQ... didn't see it just now.
    Here it is

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    In hex...
    55 h
    0F h
    --- AND
    05 h

    55 h
    0F h
    --- OR
    5F

    In decimal
    085
    255
    ---- AND
    005

    085
    255
    ---- OR
    095


    Ok, now you've got me interested. Can you explain the results from AND'ing and OR'ing with hex and decimal?

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >Ok, now you've got me interested. Can you explain the results from AND'ing and OR'ing with hex and decimal?

    try converting those hex and decimal numbers to binary and doing the and's and or's. then convert binary back to hex/decimal. you'll see bitwise operations work just the same

  13. #13
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Thanks ripper079 !

    I knew that was in there somewhere. I just sent a PM to hammer suggesting that he change the title.

    [EDIT]
    To expand on what perspective said: Everything is stored in binary. The computer doesn't care what base you use to enter or display it. You could even do bitwise operations on ASCII characters!

    The advantage of hex over decimal is that it's easier (for humans) to convert between hex and binary, than to convert between decimal and binary. You can learn to do hex-to-binary in your head, because each nybble (half-byte =4 bits) converts to a number between 0 and F.

    1010 1111 = AF Hex (1010 Always converts to A, etc)
    1111 1010 = FA Hex

    1010 1111 = 175 Decimal (Had to use a calculator)
    1111 1010 = 250 Decimal

    The advantages of hex over binary are that C++ handles hex directly. (You can't cin or cout binary). And, when your bit patterns get longer than one byte (8 bits) they get very hard to read... you have to start counting bits.

    [EDIT AGAIN]
    I used the same numbers in different bases:
    1010 1010 binary = 55 hex = 85 decimal
    0000 1111 binary = 0F (or just F) hex = 15 decimal (correct now)
    0000 0101 binary = 05 (or just 5) hex = 5 decimal
    0101 1111 binary = 5F hex = 95 decimal
    Last edited by DougDbug; 04-07-2003 at 06:03 PM.

  14. #14
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    ok I think i got it now, thanks a ton everyone! Can anyone give me a non complicated example of when you would want to use this? It still doesnt make sense to me because I cant count past 10 in binary without writing it down . Once again, tyvm for the replies!

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    I can think of an example. Suppose you had two sources in your code sending information on what the propeties your window should contain to properly display data and provide for certain required functionality for your window. Let's say the first digit is the color(blue or white), the second is the font(large or small), and the third digit is the ability to resize the window(resizable or not resizeable). You decide the appropriate action is to OR them together for the final look because if one source requires a certain background color, text size, and a resizable window, you have to have those propeties to display ALL the data properly and provide the proper functionality for the window.

    1 0 1 (from source a)
    0 0 1 (from souce b)
    -----------------------OR
    1 0 1

    Based on that, you would create a window that had a blue background with small font and a resizeable window. The alternative would be to parse each source and with a bunch of compound if-statements choose the appropriate properties. (Note: I have no experience with this stuff, but it seems like a plausible example)
    Last edited by 7stud; 04-08-2003 at 03:08 PM.

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, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM