Thread: Bitwise operators. What's this????????

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    124

    Bitwise operators. What's this????????

    I have maaaaaaaany difficulties in understanding the Bitwise Operators. In fact i understand nothing

    Here are my questions on some statements:
    Code:
    unsiged c, displayMask = 1 << 31;
    what the bold means, and what's the value of displayMask?


    Code:
    putchar( value & displayMask ? '1' : '0' );
    value is an unsigned integer.
    1) What the result of the comparison? I mean, shouldn't be something like that:
    value & displayMask == 1 ? .. : ...
    ?
    2)What does the comparison do? How it compares the bits of the two operands, and where does it know the bits of the unsigned "value"?


    Code:
    value <<= 1;
    What does this do??, what's the new value of "value"?

    Thanks in advance?
    Loading.....
    ( Trying to be a good C Programmer )

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you be doing your own homework?

    the << operator shifts N places to the left
    the >> operator shifts N places to the right

    Thus:

    a << 3

    shifts "a" three places to the left.

    The rest you should easily be able to work out. All you need is a simple printf statement and you'll have all your questions answered.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    124

    Please help...

    This is not my homework. Is the lesson of my book.

    >the << operator shifts N places to the left
    >the >> operator shifts N places to the right
    i don't understand you. What do you meen?

    And about the comparison????

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Probably the easiest way to think about this is multiplication and division by powers of 2.

    Eg.

    4 << 1 is equal to 4 * 2. (8)
    4 << 2 is equal to 4 * 4 (16)
    4 << 3 is equal to 4 * 8 (32)

    and so on... Right shifting is the same, but using division instead of multiplication.

    In computer terms however, this shifting is MUCH faster than any multiplcation / division commands.

    To understand it fully however, you need to know how a byte is stored. I feel this is beyond the scope of this forum.

    About the comparison: Anything resolving to zero always gives false. Anything non-zero would always give true, so there's no need for the "== 1" in your comparison.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    124

    Ok, but...

    Ahaaaa!!!! Thanks a lot Elixia!!!!! But i still have some BIG questions:

    1) unsiged displayMask = 1 << 31;
    This has the value 0. Why does he initialize displayMask in that way?

    2) value <<= 1;
    Meens: value = value << 1 . Which means, that if value was 20, it is now 20*2 = 40. Right?

    3) value & displayMask ( == 1 )
    how does & compare the 2 operands? What does it do? I am very confused at this point.. ( Can you give me an example with numbers to understant it please? )

    Thanks in advance..

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I don't feel that discussion of how a byte is stored is beyond the scope of this forum. It's important to know, and it isn't that hard.

    There are 8 bits in a byte. 4 High order bits (bigger ones...like the thousand place in base 10 compared to the ones place) and 4 low order bits (like the ones place)

    0000 0000

    That number, converted to base 10, is 0.

    0000 1111 converted to hex is FF - converted to base 10 it's 255.

    Each place in a binary number has different significance

    {128} {64} {32} {16} {8} {4} {2} {1}

    0 1 0 0 0 0 0 1

    So, the value of that number is 1(64)+1(1) = 65
    If you shifted all the bits one to the left, you would get
    1000 0010, the value of which is 1(128)+1(2)=130

    Hope that helps. If not, look up a tutorial on binary or something.
    Away.

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    & means 'and.'

    Here's the table for and

    Code:
        0   1
    0   0   0
    1   0   1
    If both values are set for any given bit, that bit in the result will be set. Otherwise, that bit in the result will be 0.
    Away.

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Money?
    What book are you getting these bitwise examples from? Most good C books will explain this topic at a beginner level.
    Mr. C: Author and Instructor

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't feel that discussion of how a byte is stored is beyond the scope of this forum.
    I agree, but a thorough discussion tends to be too long for any but the most interested to read. Unless you're assuming something fixed like big-endian octets.

    >There are 8 bits in a byte.
    No, there are 8 bits in an octet. The number of bits in a byte is not a fixed number and can change depending on the machine you use.

    >This has the value 0. Why does he initialize displayMask in that way?
    Bitwise operations are inherently nonportable because it requires assumptions about the binary representation of data. The person who wrote that line of code was assuming 4 byte integers, 1 << 31 being the equivalent of INT_MAX, or 2147483648. Most likely, you're running your test on a machine that uses 2 byte integers and the value overflows to 0.

    >Meens: value = value << 1 . Which means, that if value was 20, it is now 20*2 = 40. Right?
    Yes.

    >how does & compare the 2 operands?
    & compares the two operands bit by bit and returns a value with each bit set where both operands had a bit set:

    00010101 == 21
    00101101 == 45

    Those two values ANDed together would result in

    00000101 == 5

    because only the first and third bits (from the right side) are set to 1 in both operands. & means to set a bit in the result only if the same bit in both operands is set.

    >Most good C books will explain this topic at a beginner level.
    Most good C books also don't elaborate on what use this topic is good for. I've found that this is where the confusion lies.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Ok, thanks... I think i understood. But i am only at the beginning of these operators and i don't see the future very well...

  11. #11
    .........
    Join Date
    Nov 2002
    Posts
    303
    Originally posted by Prelude
    >
    Most good C books also don't elaborate on what use this topic is good for. I've found that this is where the confusion lies.
    I agree, I had tons of trouble figuring out what and how to use bitwise operators. My book only mentions them in the appendix and provides zero explanation. Then I tryed reading K&R's explanation but it still wasn't good enough for me, I didn't understand them.

    money try reading this, it explains it very well and shows how you can use them to your benefit in a program.
    http://www.gamedev.net/reference/art...rticle1563.asp

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Here's a nice example for you. C only supports numbers from 8-bits upwards. There's no direct support for numbers less than 8 bits.

    Now, lets assume your memory usage needs every byte accounted for as you have any got a few hundred of them for your entire program.

    You want two counters, but using 2 chars would be a waste, so you can do something like this:

    Code:
       char counters = 0;
    
       for (;(counters & 0x0F) < 10; counters ++)
       {
          for (;((counters & 0xF0) >> 4) < 10; counters += 0x10)
          {
             printf ("First counter: %d Second counter: %d", counters&0x0F, (counters & 0xF0) >> 4);
          }
          counters &= 0x0F;
       }
    Have a play with that and see how it works.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here is a fine article on bits and bitwise operators.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    It's a bit difficult to understand your code...

    char counters = 0;
    How do you initialize it? 0 is an integer

    0x0F?
    What's this? Does the computer understand it?

    Can you explain me how it work?

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    are you reading C How To Program by Deitel and Deitel? i had the same confusion when i first read the chapter, but you just have to reread it and reread it carefully. It's not an easy topic, but once you get it down, it's really not that hard.

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