50,523
社区成员
发帖
与我相关
我的任务
分享
/*
TODO
Implements unsigned right shift in Java. Please follow the document
comments to implement these methods.
You may need to add a main method, and some cases to tell us that your
solution works fine.
JDK is available at :
\\share.baina.com\TeamShare\JDK
Any other neccessary tools can be downloaded from the Internet. Please
don't download big guys like Eclipse or NetBeans. If you really need them,
ask IT Manager Ke WANG for them.
NOTE
If you don't know Java, you can choose to learn immediately, or use your
most familiar language to solve this problem.
NOTE
If you use other programing languages, unsigned values are NOT allowed in
your solution.
NOTE
If you use assembly, please also implement codes which allows you invoke assembly in Java.
*/
/**
* Utility for bits.
*/
public final class BitUtil {
/**
* Perform a true unsigned right shift on a {@link byte}.
* @param value The value to shift.
* @param offset The number of bits to shift.
* @return Unsigned right shift <i>offset</i> bits on the given <i>value</i>.
* <p>If offset < 0, perform left shift of <i>-offset</i> bits; if offset >=8, return 0.</p>
*/
public static final byte unsignedRightShift(final byte value, final int offset) {
if( 0< offset < 8)
{
return value>>ofset;
}
if( -8 < offset < 0)
{
return value << ofset;
}
else return 0;
}
/**
* Perform a true unsigned right shift on a {@link short}.
* @param value The value to shift.
* @param offset The number of bits to shift.
* @return Unsigned right shift <i>offset</i> bits on the given <i>value</i>.
* <p>If offset < 0, perform left shift of <i>-offset</i> bits; if offset >=16, return 0.</p>
*/
public static final short unsignedRightShift(final short value, final int offset) {
if( 0< offset < 16)
{
return value>>ofset;
}
if( -16 < offset < 0)
{
return value << ofset;
}
else return 0;
return 0;
}
/**
* Perform a true unsigned right shift on a {@link int}.
* @param value The value to shift.
* @param offset The number of bits to shift.
* @return Unsigned right shift <i>offset</i> bits on the given <i>value</i>.
* <p>If offset < 0, perform left shift of <i>-offset</i> bits; if offset >=32, return 0.</p>
*/
public static final int unsignedRightShift(final int value, final int offset) {
if( 0< offset < 32)
{
return value>>ofset;
}
if( -32 < offset < 0)
{
return value << ofset;
}
else return 0;
return 0;
}
/**
* Perform a true unsigned right shift on a {@link long}.
* @param value The value to shift.
* @param offset The number of bits to shift.
* @return Unsigned right shift <i>offset</i> bits on the given <i>value</i>.
* <p>If offset < 0, perform left shift of <i>-offset</i> bits; if offset >=64, return 0.</p>
*/
public static final long unsignedRightShift(final long value, final int offset) {
if( 0< offset < 64)
{
return value>>ofset;
}
if( -64 < offset < 0)
{
return value << ofset;
}
else return 0;
return 0;
}
}