CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2006
    Posts
    2

    Red face 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

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  5. #5
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Bit shifting from Novice C programer

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

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Bit shifting from Novice C programer

    Quote 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()!

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Bit shifting from Novice C programer

    Quote Originally Posted by Mitsukai
    no its not, std uses _X variables.
    Yes, it is:
    Quote 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]
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Mar 2006
    Posts
    2

    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

  9. #9
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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
  •  





Click Here to Expand Forum to Full Width

Featured