Thread: what does this statement do?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    what does this statement do?

    If I had a integer n, what does the statement n>>=1 do? convert n to binary, then shift to right one bit? or n stays in decimal format then gets shifted? Thanks.

  2. #2
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Convert to binary? Numbers are always stored like that. There is nu such thing as converting a variable to decimal or hex, they are always stored as binary numbers. The numerical base does only matter when you convert the int to a string.

    To answer your question: n>>=1 shifts the bits of n to the right one bit.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Binary (base 2) is just a way of representing data, as decimal (base 10) and hexadecimal (base 16) are.

    So n can be represented at a bit-level in binary, everything can.
    n >> 1 Will shift every bit in n one position right...
    for example, if n = "0100 0101".
    then after n >> 1, n = "0010 0010"
    As you notice, 0 is used as padding...

    You may like to read: http://www.cprogramming.com/tutorial...operators.html

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Being picky...

    For any integer n and b, when n >>= b is executed, n will be right shifted b bits and assigned the result. You will not notice a change in n's radix. All integers have a binary format in memory anyway.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Sorry to interject, is that the C equivalent for the ASM command SHR?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, except, the result isn't assigned.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    This leads be to a related question,

    how does cin and cout work? is << and >> not bitshift left/right? Just curious...

    I've searched for such an explanation with no results.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Depends upon if the n is signed or not.

    Code:
    n>>=1
    If n is signed:

    Code:
    mov eax, n
    sar eax, 1
    mov n, eax
    If n is unsigned:

    Code:
    mov eax, n
    shr eax, 1
    mov n, eax
    Quote Originally Posted by zacs7 View Post
    This leads be to a related question,

    how does cin and cout work? is << and >> not bitshift left/right? Just curious...

    I've searched for such an explanation with no results.
    << and >> are just operators. In C++, you can overload operators. Since it wouldn't make as much sense to bit shift streams, they've been overloaded to accept input and output, respectively.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ah, me see. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM