operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluefox
    New Member
    • Apr 2007
    • 8

    operator

    Code:
    cout <<  30 << 3 ;

    What operator "<<" is that ? i mean the one in between 30 and 3...
    and what is the difference between "<", "<<", ">", ">>" ?
    thx in advance...
  • bluefox
    New Member
    • Apr 2007
    • 8

    #2
    Code:
    int f()
    {	int a =10,b=10;
    	if(a==b)
    	{
                    	a*=3;
    	}
    	a = a << 2;
    	return a;
    }
    
    void main()
    {cout<<f();}
    I posted the wrong question, this is the original question, what does the operator in a "<<" 2 suppose to mean? and what it is called ?

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by bluefox
      Code:
      int f()
      {	int a =10,b=10;
      	if(a==b)
      	{
                      	a*=3;
      	}
      	a = a << 2;
      	return a;
      }
      
      void main()
      {cout<<f();}
      I posted the wrong question, this is the original question, what does the operator in a "<<" 2 suppose to mean? and what it is called ?
      The "<<" is a bit shift operator. It shifts the binary "1"s and "0"s by the number after it.
      So for example:
      in your case a = 30
      the binary bits for that is:
      00011110
      so if you shift the bits 2 times you get:
      01111000
      notice how the "1"s shifted to the left, that makes "a" equal 120
      Code:
      128 64 32 16       8 4 2 1
      ------------------------------------
      0   1  1  1        1 0 0 0
      0 +64+32+16  +     8+0+0+0 = 120
      Does that help?

      Comment

      • bluefox
        New Member
        • Apr 2007
        • 8

        #4
        Originally posted by ilikepython
        The "<<" is a bit shift operator. It shifts the binary "1"s and "0"s by the number after it.
        So for example:
        in your case a = 30
        the binary bits for that is:
        00011110
        so if you shift the bits 2 times you get:
        01111000
        notice how the "1"s shifted to the left, that makes "a" equal 120
        Code:
        128 64 32 16       8 4 2 1
        ------------------------------------
        0   1  1  1        1 0 0 0
        0 +64+32+16  +     8+0+0+0 = 120
        Does that help?

        sorry late reply, I lost the url to the forum...

        Hah! thanks, really helps especially on the conversion table! NEAT!

        But i hv another question....
        I changed the shift operator from "<<" to this "<"
        there's no error upon compilation, i tried to search "<" under the shift operator but no answer... you know what's that?

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          While "<<" is a bit shift operator, "<" does something completely different. "<" is the "less than" operator. It evaluates to true (i.e. a nonzero integer, usually 1) is the left hand side is less than the right hand side, and false (i.e. 0) if the right hand side is less than or equal to the left hand side. So your code

          Code:
          a = a < 3;
          will assign 1 to a if a was originally less than 3, or 0 to a otherwise.

          Comment

          • bluefox
            New Member
            • Apr 2007
            • 8

            #6
            Originally posted by Ganon11
            While "<<" is a bit shift operator, "<" does something completely different. "<" is the "less than" operator. It evaluates to true (i.e. a nonzero integer, usually 1) is the left hand side is less than the right hand side, and false (i.e. 0) if the right hand side is less than or equal to the left hand side. So your code

            Code:
            a = a < 3;
            will assign 1 to a if a was originally less than 3, or 0 to a otherwise.
            Thanks! I got all the operators mixed up!

            Comment

            Working...