I wonder how i can bitshift a double
means
double u;
double _double;
u>>_double;
Thank you, DavidD
Printable View
I wonder how i can bitshift a double
means
double u;
double _double;
u>>_double;
Thank you, DavidD
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.
Outputs 1*(2 ** 0.5) which is sqrt(2)Code:#include <cmath>
#include <iostream>
int main()
{
double u=1;
double f=0.5;
u*=std::pow(2,f);
std::cout << u;
return 0;
}
You can't. bit-shifting is defined only for integral types. double is not an integral type.Quote:
I wonder how i can bitshift a double
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.Quote:
Originally Posted by Graham
Thus it is reserved for the compiler and standard library use.Quote:
Originally Posted by Mitsukai
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:
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.Code:double u;
double _double;
u>>_double;
However, it is not a good idea to use such variables.
Yes, it is:Quote:
Originally Posted by Mitsukai
Quote:
Originally Posted by C++ Standard
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
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)