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

x/2^4 ~ x >> 4?

Printable View

- 01-18-2003Silvercordx/2^4 ~ x >> 4?
x >> 4 is the same as dividing by 2 4 times, right?

x/2^4 ~ x >> 4? - 01-18-2003Sebastiani
Why not ask your compiler:confused:

- 01-18-2003Polymorphic 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-2003foniks munkeeQuote:

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

edit: Taking into account Poly's comment, yes it is. ;) - 01-19-2003Silvercord
Padded? What does that mean.

EDIT:

Quote:

EDIT: and what datatype you're talking about.

EDIT:

Quote:

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

- 01-19-2003Polymorphic OOPQuote:

*Originally posted by Silvercord*

**Padded? What does that mean.**

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

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?**

- 01-19-2003SilvercordQuote:

No, actually that would work just as much as it would with an int. A character still represents an integer.

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

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-2003rmullen3~
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-2003rmullen3~
"(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-2003yup
>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-2003SilvercordQuote:

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? - 01-19-2003Polymorphic OOPQuote:

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

Quote:

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

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

- 01-19-2003rmullen3~
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-2003Polymorphic OOPRe: ~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