Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Shift Operators

rashu gulati
Greenhorn
Posts: 12
Hi
I need to know the answer of : -42>>-4 and also a easy way to find it out.
Also Can shift operators be applied to float,double.
Thanks
Rashu Gulati

Bert Bates
author
Sheriff
Posts: 8945
17
Hey Rashu,
Tell us what you already know about shift operators, and this particular question...
Are the operands ints or longs? Does it make a difference? What do you think the rules for floats and doubles are?
Bert

rashu gulati
Greenhorn
Posts: 12
I think that the left operand can be promoted to long or int.
Also i know something abt. <<,<<< & >> operators but cant really figure out a way to solve when some negative numbers come into picture.
Thanks

Bert Bates
author
Sheriff
Posts: 8945
17
Rashu -
Good start! So I'd start out with some easier tests like:
42 >> 1
-42 >> 1
-42 >> -1
See what results you get...
My hint is, get out your pencil and paper, and write down the bit patterns of the before and afters, and see what you can figure out. For the real exam, you'll have to understand two's complement when you're figuring out these bit-shifting questions, so this is a great place to start!

Bert Bates
author
Sheriff
Posts: 8945
17
Rashu -
Good start! So I'd start out with some easier tests like:
42 >> 1
-42 >> 1
-42 >> -1
See what results you get...
My hint is, get out your pencil and paper, and write down the bit patterns of the before and afters, and see what you can figure out. For the real exam, you'll have to understand two's complement when you're figuring out these bit-shifting questions, so this is a great place to start!