Why are those the right answers?

short s=-128
byte c=-8
1) s<<=2
2) double b=(byte) s
3) int i=-1;System. out. print (i & s)
4)((c>>1)+c)

Answers:
1)-512
2)0.0
3)-512
4)-12


0|1
0|3

Most Helpful Guy

  • Most of these are bitwise operations (although #2 is a cast) so you have to convert s and c to binary and then work them out. You should also know what a sign bit is.

    The << operator is a bitwise shift to the left. s <<= 2 is short for s = s << 2. So you're shifting s to the left by two binary digits. Because binary is base 2, every left shift is equivalent to a multiplication by two so s << 2 (on an integral value) yields the same result as s*2*2 which is indeed -512.

    #2 is a cast and Java has it's own set of rules for that which you'll probably have to look up. I don't remember them.

    #3 is sort of a trick because i is a 32 bit int and s is a 16 bit short so the result will also be a 32 bit value (I think). In this case the sign bit of s will probably be interpreted as a normal 1. I could be wrong, I'm doing this in my head.

    #4 is simple order of operations. Do the innermost brackets first and go from there. >> is a right shift which is equivalent to a division by two. c >> 1 will then be -4 and -4 + -8 = -12.

    0|0
    0|0

What Guys Said 2

  • I think you should rephrase the question and ask for help in whatever it is you are looking like "Anyone know X/Y/Z, need help?"

    0|0
    0|0
  • Because! *magic*

    Give me most helpful I want an amazong voucher!

    1|0
    0|0
    • 3mo

      dear d**k AKA blobb
      Did you really think I'd fall for it, just cause you're anonymous
      Bitch please , we've been together for 4 years :D

What Girls Said 0

No girls shared opinions.

Loading...