Click to See Complete Forum and Search --> : Bit shifting from Novice C programer


Doublegooger
March 4th, 2006, 02:34 PM
I wonder how i can bitshift a double
means

double u;
double _double;
u>>_double;

Thank you, DavidD

SuperKoko
March 4th, 2006, 02:56 PM
I don't think that bitshifting a double number has any sense, since mantissa & exponent would conflict.

However, you can multiply a floating point number by a power of two.

e.g.

#include <cmath>
#include <iostream>

int main()
{
double u=1;
double f=0.5;
u*=std::pow(2,f);
std::cout << u;
return 0;
}

Outputs 1*(2 ** 0.5) which is sqrt(2)

cilu
March 4th, 2006, 04:39 PM
I wonder how i can bitshift a double
You can't. bit-shifting is defined only for integral types. double is not an integral type.

Graham
March 4th, 2006, 05:27 PM
Also, how can you shift by a fractional number of bits?

Oh, and leading underscores in names are reserved for the compiler's use, so you shouldn't use them in your programs.

Mitsukai
March 4th, 2006, 05:52 PM
Also, how can you shift by a fractional number of bits?

Oh, and leading underscores in names are reserved for the compiler's use, so you shouldn't use them in your programs.
no its not, std uses _X variables.

SuperKoko
March 5th, 2006, 02:09 AM
no its not, std uses _X variables.

Thus it is reserved for the compiler and standard library use.
It allows compilers to have non-standard extensions without interference with user's code.
It also allows compilers to name fields in the standard library, to avoid conflicts if someone derive from the class (even private variables may conflict).

Here are the rules:
Any variable starting with two underscores (or more) or starting with an underscore followed by a capital letter is reserved everywhere by the compiler (like this _X variable). It means that the compiler can use it as a keyword.

Any variable starting with an underscore (or more), is reserved as name in global namespace and in namespace std.
Thus, this code:

double u;
double _double;
u>>_double;

Works! Even if there is a function or variable named _double in the global namespace or std namespace, automatic variables can hide gracefully such identifiers.
However, it is not a good idea to use such variables.

cilu
March 5th, 2006, 03:10 AM
no its not, std uses _X variables.
Yes, it is:

17.4.3.1.2 - Global names [lib.global.names]
-1- Certain sets of names and function signatures are always reserved to the implementation:

Each name that contains a double underscore ("__") or begins with an underscore followed by an uppercase letter (lex.key) is reserved to the implementation for any use.
Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.*

[Footnote: Such names are also reserved in namespace ::std (lib.reserved.names). --- end foonote]

Doublegooger
March 5th, 2006, 04:22 AM
Thanks every1 fo your kind replies,

I am still wondering
double u;
int p,d=2;
std::cin>>u;
p>>d;
that means U is streaming in and shift P to D 2 bits right ?
So what is the difference between underlying stream secret and bitshifting ?

Thanks
Davidd

SuperKoko
March 5th, 2006, 05:05 AM
C++ Streams overloads operator>>.
And, overloading can be done with any C++ operator and any C++ type, provided that at least one operand of the operator is a class, enum or union type.
The semantics of the overloaded operator need not to be the same than the original operator (although, it is a good programming practice to make it meaningful and intuitive).
So, there is nothing common between std::istream& std::istream::operator>>(double&) and an hypotetical inexistent double operator>>(double, int)