x >> 4 is the same as dividing by 2 4 times, right?
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?...
x >> 4 is the same as dividing by 2 4 times, right?
x/2^4 ~ x >> 4?
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; }
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-19-2003 at 12:03 AM.
x >> n is the same as dividing by 2 to the nth power. So yes, it is.x >> 4 is the same as dividing by 2 4 times, right?
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."
Padded? What does that mean.
EDIT:
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: and what datatype you're talking about.
EDIT:
because only silly people talk to inanimate objectsWhy not ask your compiler
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-19-2003 at 12:07 AM.
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
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
Padded? What does that mean.
No, actually that would work just as much as it would with an int. A character still represents an integer.Originally posted by Silvercord
I'm not dumb enough to try to divide a character by 2 any number of times...
I was referring to a floating point datatype IE float, double, long double
nope, refer above.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?
I actually knew that, honestly, stop looking at me funny!No, actually that would work just as much as it would with an int. A character still represents an integer.
www.asciitable.com
Okay, now THAT makes perfect sense, umm, you would always add a zero, wouldn't you?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).
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-19-2003 at 12:31 AM.
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
"(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
>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
ok good because it looks really stupid and im already having to put up with looking at myself in my glass bedroomif 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
EDIT: did i get all those calculations correct? I hope I did...what about those binary points?
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.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?
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.
No, 126.5 in binary point format would be 1111110.1Originally posted by Silvercord
126.5 == 01111110.101 ?
Is that right? if not how in the heck do you do binary digits?
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)
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)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)
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
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.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