x/2^4 ~ x >> 4?

This is a discussion on x/2^4 ~ x >> 4? within the C++ Programming forums, part of the General Programming Boards category; x >> 4 is the same as dividing by 2 4 times, right? x/2^4 ~ x >> 4?...

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    x/2^4 ~ x >> 4?

    x >> 4 is the same as dividing by 2 4 times, right?

    x/2^4 ~ x >> 4?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why not ask your compiler
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Depends on what the data is padded with from the left when you right shift.

    EDIT: and what datatype you're talking about.
    Last edited by Polymorphic OOP; 01-18-2003 at 11:03 PM.

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    x >> 4 is the same as dividing by 2 4 times, right?
    x >> n is the same as dividing by 2 to the nth power. So yes, it is.

    edit: Taking into account Poly's comment, yes it is.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  5. #5
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Padded? What does that mean.

    EDIT:
    EDIT: and what datatype you're talking about.
    I'm not dumb enough to try to divide a character by 2 any number of times...well yes I am I just haven't thought to try it yet!

    EDIT:
    Why not ask your compiler
    because only silly people talk to inanimate objects

    EDIT: but yeah anyway PolymorphicOOP you meant that some data types aren't big enough/accurate enough to be divided by two four times, right?
    Last edited by Silvercord; 01-18-2003 at 11:07 PM.

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    I believe he meant floating point values (float, double), they cannot be shifted, ANDed, ORed, XORed, modulus-ed, etc...
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Silvercord
    Padded? What does that mean.
    If you are shifting to the right then some values have to be put in to fill in the spaces on the left (0 or 1). That's padding (not to be confused with structure padding).

    Originally posted by Silvercord
    I'm not dumb enough to try to divide a character by 2 any number of times...
    No, actually that would work just as much as it would with an int. A character still represents an integer.

    I was referring to a floating point datatype IE float, double, long double

    Originally posted by Silvercord
    but yeah anyway PolymorphicOOP you meant that some data types aren't big enough/accurate enough to be divided by two four times, right?
    nope, refer above.

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    No, actually that would work just as much as it would with an int. A character still represents an integer.
    I actually knew that, honestly, stop looking at me funny!
    www.asciitable.com

    If you are shifting to the right then some values have to be put in to fill in the spaces on the left (0 or 1). That's padding (not to be confused with structure padding).
    Okay, now THAT makes perfect sense, umm, you would always add a zero, wouldn't you?
    11111101 >> 1 == 01111110 ?

    Okay I'm going to do this out to make sure for myself, and you guys can check my calculations

    11111101 == 1 + (2 * 0) + (4) + (8) + (16) + (32) + 64 + 128 = 253

    assuming that is correct, then if you shift if, you must add a zero otherwise it seems it would change the value, so anyway 253 / 2 is like 126.5 (uummm, I dont' know how to do binary digits, crap)

    011111110 == (1 * 0) + (2) + (4) + (8) + (16) + (32) + (64) + (0 * 128) = 126

    126.5 == 01111110.101 ?
    Is that right? if not how in the heck do you do binary digits?

    EDIT: I think we should drop base 10 and do base 2, PolymorphicOOP: we should start a binary number system cult!

    EDIT1: What is the point in doing all this shifing stuff? Why not just do x / 2; in your code? Does it have to do with the fact that computers are already in binary mode, and that in order to do that calculation it would have to translate the numbers to base 10, divide them, then translate them back into base 2? (this would mean a speed boost I would think but I think it's still stupid)
    Last edited by Silvercord; 01-18-2003 at 11:31 PM.

  9. #9
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    I assume you mean decimals. Binary decimals don't exist... numbers are always strings of ones and zeros.

    A floating point number is made up of a sign, a mantissa, and an exponent.

    Integers don't store decimals.

    253 >> 1 == 126
    253.0 / 2 == 126.5
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  10. #10
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    "(this would mean a speed boost I would think but I think it's still stupid)"

    The speed boost isn't stupid. Binary shifts are quite faster than multiplication and division, although the speed of floating point calculation is becoming faster every day it seems.

    It all comes down to optimization, assuming your compiler doesn't do these for you:

    int a, b;
    a = b / 2;

    is slower than
    a = b * 0.5;

    which is slower than the fastest
    a = b >> 1;
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  11. #11
    yup
    Guest
    >What is the point in doing all this shifing stuff? Why not just do >x / 2; in your code?


    if you put x/2 in your code, it will be the same as shifting after you have compiled (for any decent compiler that is). so there is no performance adventage with shifting and it looks ugly

  12. #12
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    if you put x/2 in your code, it will be the same as shifting after you have compiled (for any decent compiler that is). so there is no performance adventage with shifting and it looks ugly
    ok good because it looks really stupid and im already having to put up with looking at myself in my glass bedroom

    EDIT: did i get all those calculations correct? I hope I did...what about those binary points?

  13. #13
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Silvercord
    I actually knew that, honestly, stop looking at me funny!
    [url]Okay, now THAT makes perfect sense, umm, you would always add a zero, wouldn't you?
    No, not necissarily -- that would work for a positive number, and that's assuming it's using a 2's compliment system or something that acts similarly for positive numbers, which isn't guaranteed.

    If you had a negative number in the 2's compliment system, you'd have to pad by 1's. Even then it's not exactly the same as division by powers of 2. Why? Because that would result in numbers being rounded away from 0, which doesn't happen with normal division. If it's not the 2's compliment system, then there can be more or less differences.

    Originally posted by Silvercord
    126.5 == 01111110.101 ?
    Is that right? if not how in the heck do you do binary digits?
    No, 126.5 in binary point format would be 1111110.1

    Anyways, that's not how non-integer numbers are stored in C++. They are stored in binary scientific notation (with a sign, exponent with sign, and mantissa)

    Originally posted by Silvercord
    What is the point in doing all this shifing stuff? Why not just do x / 2; in your code? Does it have to do with the fact that computers are already in binary mode, and that in order to do that calculation it would have to translate the numbers to base 10, divide them, then translate them back into base 2? (this would mean a speed boost I would think but I think it's still stupid)
    Back before compilers were optimized, x >> 1 would result in faster code. Division on numbers is taxing while shifting is not. Nowadays, most compilers will notice that if you do x / 2 you are dividing by a power of two and will internally implement it as a right shift (if that follows with their method of storing data)

  14. #14
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Sigh... yes, your compiler will most likely optimize for you, but don't completely discount the concept of shifting. It's still a very important functionality and concept to know.

    Besides I don't see how

    x /= 2;

    is any "better looking" than

    x >>= 1;

    =P
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  15. #15
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: ~

    Originally posted by rmullen3
    Sigh... yes, your compiler will most likely optimize for you, but don't completely discount the concept of shifting. It's still a very important functionality and concept to know.

    Besides I don't see how

    x /= 2;

    is any "better looking" than

    x >>= 1;

    =P
    Shifting isn't guaranteed to be equivalent to division by powers of two. Your code is only guaranteed to be portable if you use division. You're best off relying on your compiler for that particular optimization, as it may not work on other compilers.

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

Similar Threads

  1. Serializing problem.. (can't use >> operator)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2005, 11:10 AM
  2. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 04:01 PM
  3. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 04:33 PM
  4. overloaded >> operator problem
    By quizkiwi in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2005, 03:27 PM
  5. C++ >> Standard Library >> Header File
    By hrk2006 in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2003, 08:30 PM

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