Menu

Use of "<<" operator...

2007-05-26
2012-09-26
  • Nobody/Anonymous

    I'm reading Bruce Eckles book "Thinking in C++ Vol1". I'm early in the book and he uses this piece of code:

    //: C03:printBinary.cpp {O}

    include <iostream>

    void printBinary(const unsigned char val) {
    for(int i = 7; i >= 0; i--)
    if(val & (1 << i))
    std::cout << "1";
    else
    std::cout << "0";
    } ///:~

    I understand what the code is supposed to do: check each bit in a byte to see if it's 0 or 1. And Eckles says that (1 << i) "produces a one in each successive bit position; in binary: 00000001, 00000010, etc." But I don't see how it does that. Why couldn't he have used (val == 1) instead?

     
    • Nobody/Anonymous

      Never mind. I get it.

       
      • Wayne Keen

        Wayne Keen - 2007-05-27

        You may not have intended it this way, but leaving this thread hanging with:

        "Never mind. I get it"

        is kind of selfish on your part. Given the opportunity to give instead of
        taking, you failed.

        A considerate person asking a question always endeavors to make sure that the
        thread is complete - even if he/she figured it out on their own with no help.

        The way you accomplish this is by following up with:

        I figured it out by myself, here is the way it works ...

        Wayne

         
    • Nobody/Anonymous

      So why don't you explain how it works, drwayne?

      Explaining as decimal
      x << n
      is the equivalent of x * power(2, n)
      So 2 * power(2, 2) = 8

      Explaining as binary
      << shifts left
      So
      binary
      2 --> 001010 << 2
      returns
      8 <-- 101000
      All bits are pushed n places to the left.

       
    • Anonymous

      Anonymous - 2007-05-27

      >> So why don't you explain how it works, drwayne?
      Because the explanation is already there in Bruce Eckels' book perhaps. I imagine that is how he finally "got it". I am not sure what your explanation brings to the party, and the "explaining it as decimal" part, in the context of the original code merely obfuscates the issue.

      I imagine that the OP understood as much as you explained, but merely missed the fact that the (val<<i) expression was done within an iterative loop.

       
    • MoisterOyster

      MoisterOyster - 2007-05-27

      Well, okay. I didn't mean to be rude. I just figured everybody but me knew the answer.

      My first problem was not understanding that the insertion operator was now being used as a "shift left" operator. So, I was trying to understand it as somehow "i" inserts itself as a "1". After I posted here I reviewed the paragraph and cleared up that much.

      But I was still stuck as to how 1 was able to determine the value of the bit. So, after puzzling over this I realized that a byte representing "1" was written out like this:

      00000001

      So, if (i == 7) in the conditional expression, you shift the "1" bit seven spaces, which results in:

      10000000

      Now; if you "&" that to any other binary byte all the bits will result in zero EXCEPT the high bit. Which will result in the state of the high bit in the byte being compared. Thus, do we determine the bit value for the high order bit.

      Next the value of "i" is decremented to the value "6". So we shift the one six spaces and compare the next high order bit.

      This process is repeated until we just compare the low order bit and after that the conditional expression is false.

      I wish Eckle explained it in detail like that. It would have saved me an hour.

      Anyway, I'm about to post another problem I encountered, so maybe you can help me with that .

      Thanks.

       
    • Anonymous

      Anonymous - 2007-05-28

      I did not realise that the "So why don't you explain how it works, drwayne?" response was from the OP, as now appears to be the case; had that been apparent, my response would have been different. Consider signing your posts or get a Sourceforge login to avoid confusion.

      >> the insertion operator was now being used as a "shift left" operator.

      Yikes, you are (or perhaps were) confused. Most people get thrown by the use of the "shift left" operator as the "insertion operator". The left shift operator is the << symbols built-in function. It is overloaded by the ostream class to implement the "insertion operator". An operator is not merely defined by its glyph, but also by its operands. The << is only an "insertion operator" when the left hand operand is an std::ostream object (such as cout).

      >> I wish Eckle explained it in detail like that. It would have saved me an hour.

      I admit I have not read the book in detail. I have an electronic copy; it would be helpful if you could provide a page reference to this example. Normally in a book, to avoid repetition, the detail you seek will have been covered in earlier chapters or paragraphs. In some cases the author presumes some prior knowledge such as C programming. Some writers are just more succinct that you would wish, others perhaps ate just vague and incomplete. Perhaps it is because I am an embedded systems developer and this stuff (bit manipulation) is second nature to me, that I find your explanation a little tortuous, had Mr Eckel laboured the point that much I would soon get very bored.

      Clifford

       
    • MoisterOyster

      MoisterOyster - 2007-05-28

      No Clifford, that response was not mine. I wouldn't speak like that to another person in an online forum. Leads to flame wars. I do have a login but I was on another computer when I first posted on this thread and didn't realize I wasn't logged in.

      Yes, I understand your point about this symbol actually being the shift left op and only later the insertion op. But, I'd never studied C per se, I started with C++ which only gives brief recaps of C as necessary. Since bitwise operators are usually only covered later, I was introduced to "<<" as the insertion op first.

      I also am using the electronic copy of the Eckle book. So, I don't know the page number, just the chapter - it is the chapter called "The C in C++"

      At the end of the chapter he says that if his recap of C was difficult then a reader should refer to his "Thinking in C foundation" lecture which is here:

      http://personales.unican.es/corcuerp/ThinkC/Home.htm

      So, I am going through that before continuing.

      Anyway, thanks all for the replies.

       
      • Anonymous

        Anonymous - 2007-05-28

        It seems that you are going about this in entirely the right way, perhaps just a little quick in this instance in firing off a post. It happens. Good luck.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.