x >> 4 is the same as dividing by 2 4 times, right?
x/2^4 ~ x >> 4?
Printable View
x >> 4 is the same as dividing by 2 4 times, right?
x/2^4 ~ x >> 4?
Why not ask your compiler:confused:
Depends on what the data is padded with from the left when you right shift.
EDIT: and what datatype you're talking about.
x >> n is the same as dividing by 2 to the nth power. So yes, it is.Quote:
x >> 4 is the same as dividing by 2 4 times, right?
edit: Taking into account Poly's comment, yes it is. ;)
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!Quote:
EDIT: and what datatype you're talking about.
EDIT:
because only silly people talk to inanimate objectsQuote:
Why 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?
I believe he meant floating point values (float, double), they cannot be shifted, ANDed, ORed, XORed, modulus-ed, etc...
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).Quote:
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.Quote:
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.Quote:
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!Quote:
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?Quote:
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)
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
"(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;
>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 bedroomQuote:
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
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.Quote:
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.1Quote:
Originally 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)Quote:
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
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.Quote:
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