实时面试题 快来人

tan625747 2010-04-09 03:56:02
/*
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) {

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) {
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) {
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) {
return 0;
}

}



快点啊,越快越好

...全文
92 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tan625747 2010-04-09
  • 打赏
  • 举报
回复


/*
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;
}

}

tan625747 2010-04-09
  • 打赏
  • 举报
回复
没高手吗?

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧