|
-
March 4th, 2006, 03:34 PM
#1
Bit shifting from Novice C programer
I wonder how i can bitshift a double
means
double u;
double _double;
u>>_double;
Thank you, DavidD
I am a good man
-
March 4th, 2006, 03:56 PM
#2
Re: Bit shifting from Novice C programer
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.
Code:
#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)
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
March 4th, 2006, 05:39 PM
#3
Re: Bit shifting from Novice C programer
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.
-
March 4th, 2006, 06:27 PM
#4
Re: Bit shifting from Novice C programer
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 4th, 2006, 06:52 PM
#5
Re: Bit shifting from Novice C programer
 Originally Posted by Graham
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.
-
March 5th, 2006, 03:09 AM
#6
Re: Bit shifting from Novice C programer
 Originally Posted by Mitsukai
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:
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.
Last edited by SuperKoko; March 5th, 2006 at 03:11 AM.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
March 5th, 2006, 04:10 AM
#7
Re: Bit shifting from Novice C programer
 Originally Posted by Mitsukai
no its not, std uses _X variables.
Yes, it is:
 Originally Posted by C++ Standard
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]
-
March 5th, 2006, 05:22 AM
#8
Re: Bit shifting from Novice C programer
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
I am a good man
-
March 5th, 2006, 06:05 AM
#9
Re: Bit shifting from Novice C programer
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: perator>>(double&) and an hypotetical inexistent double operator>>(double, int)
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|