Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# 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: 8900
5
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: 8900
5
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: 8900
5
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!