# x/2^4 ~ x >> 4?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-18-2003
Silvercord
x/2^4 ~ x >> 4?
x >> 4 is the same as dividing by 2 4 times, right?

x/2^4 ~ x >> 4?
• 01-18-2003
Sebastiani
• 01-18-2003
Polymorphic OOP
Depends on what the data is padded with from the left when you right shift.

EDIT: and what datatype you're talking about.
• 01-19-2003
foniks munkee
Quote:

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. ;)
• 01-19-2003
Silvercord
Padded? What does that mean.

EDIT:
Quote:

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:
Quote:

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?
• 01-19-2003
rmullen3
~
I believe he meant floating point values (float, double), they cannot be shifted, ANDed, ORed, XORed, modulus-ed, etc...
• 01-19-2003
Polymorphic OOP
Quote:

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).

Quote:

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

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?
nope, refer above.
• 01-19-2003
Silvercord
Quote:

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

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).
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)
• 01-19-2003
rmullen3
~
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
• 01-19-2003
rmullen3
~
"(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;
• 01-19-2003
yup
>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
• 01-19-2003
Silvercord
Quote:

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?
• 01-19-2003
Polymorphic OOP
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?

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.

Quote:

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)

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)
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)
• 01-19-2003
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
• 01-19-2003
Polymorphic OOP
Re: ~
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

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last