English 中文(简体)
2. 转售一名麻醉剂
原标题:resizing a byte[] two s complement represented integer

I have a two s complement representation of a number, in a byte array, and I want to expand it to use a larger byte array. (You can get two s complement byte[] out of BigIntegers)

So my new byte array must have the same sign bit (ie highest bit) and then everything else shifted.

因此:

byte[] resize (byte[] raw, int len)
{
byte high = raw[0];

resized[0] = high & 0x80 //ie binary 1000 0000
raw[0] = high & 0x7F // 0111 1111
//causes a side effect but i don t care raw is a throw away value

byte[] resized = new byte[len];
    system.ArrayCopy(raw,0,resized,len-raw.length, len);
}

Am I on the right track? I m having trouble getting my head around, byte length hex literals work. Or do my hex literals have to be int sized? If so I m going to have to cast my everything.

:

resized[0] = (byte) ((int)high & 0x8000)
raw[0] = (byte)((int)high & 0x7FFF)

The equivalent?

最佳回答

Step 1: save the sign bit (i.e: the Most Significant bit)
Step 2: the new padding bits should be the same as the sign bit. Example:

1 byte: 11110001 
2 bytes: 1111111111110001
8 bytes: 1111111111111111111111111111111111111111111111111111111111110001

积极价值相同:

1 byte: 00001111
2 bytes: 0000000000001111
8 bytes: 0000000000000000000000000000000000000000000000000000000000001111

通知说,在进行双向交易时,会鼓励 by。 因此,根据签字,婚礼为00h或FFh。 其余则与原样相同。

标识如下:

//If the MSB were raw[0]:
boolean isNegative = false;
if(raw[0] & 0x80 != 0){
      isNegative = true;
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签