By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,154 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,154 IT Pros & Developers. It's quick & easy.

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

P: 1
Expand|Select|Wrap|Line Numbers
  1. //like this. please answer.....
  2. int main()
  3. {
  4.     float num = 4.2;
  5.     printf(" %f\n",(num << 1));
  6. }
Oct 1 '10 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,916
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
Oct 1 '10 #2

Expert 100+
P: 2,400
What would you like "4.2 << 1" to do?
Oct 1 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.