Is "long x = 1/2" equal to 1 or 0, and why?

user69514 picture user69514 · Feb 2, 2010 · Viewed 8.8k times · Source

if I have something like:

long x = 1/2;

shouldn't this be rounded up to 1? When I print it on the screen it say 0.

Answer

tvanfosson picture tvanfosson · Feb 2, 2010

It's doing integer division, which truncates everything to the right of the decimal point.