Thread: cout technically isn't a part of C++

  1. #1
    Shadow12345
    Guest

    cout technically isn't a part of C++

    If the only think u know how to do is 'cout << ' with text in C++, then all you really know how to do is use the bitshift operator because cout isn't a part of the C++ programming language, it's just an object of output stream class declare in iostream...right? Even if something is in the stl that doesn't make it an intrinsic part of the C++ programming language, intrinsic parts of the language are really only the keywords, templates, pointers, references, stuff like that. Right? Or should I just be slapped?

    EDIT:
    You would also know how to include header files and use main() if you could do 'cout <<'...obviously

    EDIT1:
    Whoa, the output stream has a lot of members...this is sweet
    Last edited by Shadow12345; 10-01-2002 at 12:22 PM.

  2. #2
    >>then all you really know how to do is use the bitshift operator

    Except thats not bit shifting. That is the bit shifting operator, true, but thats not its function in this case. cout overloads those operators for its own use.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    uh...

    operator>> in that context is called the insertion operator and operator>> in the cin context is known as the extraction operator (i may have flipped those around because i haven't called them by their names in such a long time).

    And saying cout isn't part of C++ is like saying printf isnot part of C.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The function of cout is specified in the ANSI/ISO standard. Thus, it is part of the C++ language.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Shadow12345
    Guest
    works for me

    then how do you use the bitshift operator

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >then how do you use the bitshift operator
    Very carefully.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Shadow12345
    Guest
    but...what exactly does it do? I've seen it used before other than with cout, but I didn't know what its purpose was. Why do you need to 'shift bits', why can't we just leave the poor bits alone?

  8. #8
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Bit shifting is a good way to do some very fast math, that is... if you're good with base-2/8/16 math

  9. #9
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    It can also be used for something like this:
    Code:
    #include <iostream> 
    
    int main()
    {
        int counter, num;
    
        std::cout << "Enter a number: ";
        std::cin >> num;
    
        std::cout << num << " is binary: ";
    
        for (counter=8; counter>=0; counter--)
            std::cout << ((num >> counter) & 1);
        std::cout << std::endl;
    
        return 0;
    }
    If you did not know what the shift right operator was, then this would probably confuse you

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Bits are the individual 1's and 0's that the computer uses to store information.

    Binary means base 2.

    Bit-shifting is a binary operation.

    So, if I have 00000111 binary (Equal to 7 decimal) and I bit-shift one position, I will have 00001110 binary (Equal to 14 decimal).

    Every shift-left multiplies the number by two. Every shift-right divides by two. I think that's why BMJ says "fast easy math".

    This binary stuff is used by us engineers where a single bit is set (to one) or cleared (to zero) by a switch or something.

    If you want to learn about this stuff you'd need to look into "binary math" and boolian logic". Since it's easy to convert between hexadecimal (base 16) and binary, and it's easier (for us humans) to read and write hex, a lot of this stuff is coded in hex.

  11. #11

  12. #12
    >then how do you use the bitshift operator
    Very carefully.
    lol. How so? Unless of course you mean the obvious mistake of being an idiot and accidentally replacing 'x / 2' with 'x >> 2' because, maybe its 4 am and you've been coding for 19 hours straight... no, honest, I've never done that.


    >>why can't we just leave the poor bits alone?

    They're just asking for it.

    Other uses include something like this which I use on a fairly regular basis:

    //These #defines retrieve R,G,B, or A values from a DWORD (D3DCOLOR)
    #define RetrieveB(rgb) ((BYTE)(rgb))
    #define RetrieveG(rgb) ((BYTE)(((WORD)(rgb)) >> 8))
    #define RetrieveR(rgb) ((BYTE)((rgb)>>16))
    #define RetrieveA(rgb) ((BYTE)((rgb)>>24))

    (And nobody whine about using macros instead of inline functions. Theres nothing wrong with macros if you're careful. The only problems lie in carelessness.)
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  13. #13
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Why are you using macros? You should be using inline functions

  14. #14
    *grrr* I should have expected that.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  15. #15
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    "Theres nothing wrong with macros if you're careful."

    But rather than macros that look like macros (alerting reader not to do stuff like side effects in the arguments) they look like ordinary functions. Given, everyone's style is different, but I'd say caps for macros is really widespread and good practice.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiles but won't cout?
    By Kayoss in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2006, 01:27 PM
  2. Can someone look over my code? part 2
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-18-2006, 06:33 AM
  3. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM
  4. FAQ: Cout and textcolor question...
    By Laniston in forum FAQ Board
    Replies: 2
    Last Post: 12-19-2002, 10:54 AM
  5. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM