Can << and >> operator works on float variable?If not then why?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • avixt
    New Member
    • Mar 2010
    • 1

    #1

    Can << and >> operator works on float variable?If not then why?

    Code:
    //like this. please answer.....
    int main()
    {
    	float num = 4.2;
    	printf(" %f\n",(num << 1));
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The shift << and >> operators only work on integer types. They actually shift the bits what make up the value left or right.

    You can't do that to float or double or long double variables because the internal structure of those variables, how the bits are used is such that shifting them makes no logical sense.

    Try reading What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What would you like "4.2 << 1" to do?

      Comment

      Working...