• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bitshift problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this...

int i = 4*6-3/2<<2*5>>>1%2-4^3;
According to me, arithmetic operations preceed over binary shift operators , taking this into account, the answer to i is 0 but when I run , I get an answer 3.

This is how I evaluated.
int i = ((4*6)-(3/2))<<2*5>>>((1%2)-(4^3))
int i = 23<<10>>>-2
int i = 23552>>>-2
int i = 0

Now, what is wrong ???
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jayashree Mohan:
See this...

int i = 4*6-3/2<<2*5>>>1%2-4^3;
According to me, arithmetic operations preceed over binary shift operators , taking this into account, the answer to i is 0 but when I run , I get an answer 3.

This is how I evaluated.
int i = ((4*6)-(3/2))<<2*5>>>((1%2)-(4^3))
int i = 23<<10>>>-2
int i = 23552>>>-2
int i = 0

Now, what is wrong ???



Of cource ,it's wrong .
PRI : Unary > Arithmetic > Relational > Logical > Conditional > Assignment.
*/+-<<>>> are Arithmetic ,and ^ is Logical .
SO:
int i = 24-1<<10>>>2-4^3
i = 23<<10>>2-4^3
i = 23<<10>>-2^3
i = 0^3
I hope it can help you.
Leoo yu
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In this
4*6-3/2<<2*5>>>1%2-4^3;

Arithmetic operators is given high priority.

So
Step1: 23<<10>>>-3^3
Step2: 2352>>>-3^3
Step3: 0^3
Step4: 3

Hence the result is 3.

Regards,
Venkat
reply
    Bookmark Topic Watch Topic
  • New Topic