I have two bytes of data. I converted each of them to Uint8 then I produced a Uint16 from them.
How I can produce two's complement of this Uint16 number?
I've tried uInt16 = ~uInt16 + 1 but the code produces 32bit integer and I want it to stay a 16bit Integer.
byte firstByte, secondByte;
int firstUint8, secondUint8, uInt16;
firstByte = buffer[index];//get first byte from buffer
secondByte = buffer[index + 1];//get second byte from buffer
firstUint8=firstByte & 0xFF;//produce Uint8
secondUint8 = secondByte & 0xFF;//produce Uint8
uInt16 = 256 * firstUint8 + secondUint8;//create Uint16 from these to Uint8
twosComplementOfUInt16=~number+1; //produce 32 bit integer but I want int16
Java is not the best programing language to work with bits. But if you want you can read the documentation to see how the number are represented in java; how to work with bytes or you can do a tutorial.
As observation the (~ and +) returns an integer
public static void main(String[] args) {
int uint8 = 0xff;
int uint16 = 0xffff;
long uint32 = 0xffffffff;
int one = 0x0001;
int ten = 0x000A;
int twoComplementOfTen = 0xFFF6;
int computedTwoComplementOfTen = ~ten + one;
int revertTwoComplementOfTen = ~twoComplementOfTen + one;
System.out.printf("One = 0x%04X \n", one);
System.out.printf("ten = 0x%04X \n", ten);
System.out.printf("~ten + one = 0x%04X \n", twoComplementOfTen);
System.out.printf("Computed ~ten + one = 0x%04X \n", computedTwoComplementOfTen);
System.out.printf("~twoComplementOfTen + one = 0x%04X \n", revertTwoComplementOfTen);
System.out.printf("Computed ~ten + one with uint16 mask = 0x%04X \n", uint16 & computedTwoComplementOfTen);
System.out.printf("~twoComplementOfTen + one with uint16 mask = 0x%04X \n", uint16 & revertTwoComplementOfTen);
}
Output:
One = 0x0001
Ten = 0x000A
~ten + one = 0xFFF6
Computed ~ten + one = 0xFFFFFFF6
~twoComplementOfTen + one = 0xFFFF000A
Computed ~ten + one with uint16 mask = 0xFFF6
~twoComplementOfTen + one with uint16 mask = 0x000A
The "twos complement" of a number is obtained by negation, at least on machines that use twos-complement representation of integers, which is true for nearly all modern hardware and is true for the Java virtual machine.
short x;
...set value of x...
x = -x;
In twos-complement hardware and in the Java virtual machine, negation is equivalent to invert and add one. The following demonstrates this:
Example:
public class Foo {
public static void main(String[] args) {
short n = 2; n = -n;
System.out.println(n);
short m = 2; m = ~m + 1;
System.out.println(m);
}
}
The output from the above is identical for m and n.
If you find it necessary to use 32-bit ints for the value then you can simply mask the result to 16 bits.
int uint16 = some_value;
int compl = -uint16 & 0xffff;
Related
I want to write/read millions of numbers from 0-15 from/to a file. Precision is not an issue, as long as the read values are within +-0.1 of the written one, everything is fine.
Previous Ideas
My first, premature idea was to convert a float to a String like this and write them space-separated:
String.format("%.1f%s", float)
This, of course, is very inefficient, as it uses 4-5 bytes for every float.
Then I came to the idea to just write the bytes of each float, that would be faster but not sufficiently reduce the size.
ByteBuffer.allocate(4).putFloat(float).array()
Current issue
My current idea is to reduce the float to one byte. Looking at the ranges and precision I need, I would allocate the first 4 bits to represent the decimals before the floating point and the last 4 bits to the tail.
But how can I obtain these bits fast, since it has to be done millions of times?
Since your tail is single digit, it can be implicit - i.e. 14.3 is converted to 143. To convert back, it would simply be 143 / 10 to get whole part, and 143 % 10 to get the fraction. Here is how an implementation could look like.
public class Test {
public static void main(String[] args) {
float floatValue = 14.1f;
Test test = new Test();
byte b = test.toByteStorage(floatValue);
System.out.println(test.fromByteStorage(b));
}
byte toByteStorage(float f) {
return (byte) (f * 10);
}
String fromByteStorage(byte b) {
int intValue = Byte.toUnsignedInt(b);
return intValue / 10 + "." + intValue % 10;
}
}
You might use something like this:
// conversion factor to map 15.0 to 0xF0
float m = (float) (0xF0 / 15.0);
for (float x = 0.0f; x <= 15.0f; x += 0.25f) {
// obtain byte corresponding to float
byte b = (byte) (m * x);
// recover float from byte to check conversion
// mask off sign bit to convert signed to unsigned byte
float r = (b & 0x0FF) / m;
// what is the difference between original float and
// recovered float?
float error = Math.abs(x - r);
// show all values for testing
String s = " b=0x" + Integer.toHexString(b & 0x0FF) +
" r=" + Float.toString(r) +
" err=" + Float.toString(error);
System.out.println(s);
}
I'm attempting to create a java simulator that will simulate all the possible signed multiplications for 4 bit numbers.
My code layout uses chars to represent the actual bits.
My issue I have now is splitting up my int inputs (multiplicand/multiplier) bit by bit to place each bit into a new char so I can manipulate each char (ie bit) to perform signed multiplication.
Any pointers are greatly appreciated
public class SignedMultiplication {
int multiplicand, multiplier, expected_product, actual_product;
char md_bit0, md_bit1, md_bit2, md_bit3, mr_bit0, mr_bit1, mr_bit2, mr_bit3;
char pr_bit0, pr_bit1, pr_bit2, pr_bit3, pr_bit4, pr_bit5, pr_bit6, pr_bit7;
for(multiplicand=-8;multiplicand<8;++multiplicand){
// set multiplicand bits for two's complement
md_bit0 = ;
md_bit1 = ;
md_bit2 = ;
md_bit3 = ;
for(multiplier = -8; multiplier < 8; ++multiplier){
// set multiplier bits for two's complement
mr_bit0 = ;
mr_bit1 = ;
mr_bit2 = ;
mr_bit3 = ;
// perform 4 bit multiplier operation
// result is in pr_bit0 through pr_bit7
// set actual product to value converting 2's complement to an integer
actual_product = ;
expected_product = multiplicand * multiplier;
if( expected_product != actual_product ){
System.out.println("Multiplicand: " + multiplicand + " Multiplier: " + multiplier +
" Expected Result: " + expected_product + " Obtained: " + actual_product );
}
}
}
}
I have 2 byte arrays, each containing 4 bytes (byte1[], byte2[]) and I want to XOR them to create a new 4 byte array (byte3[]) how would I do this?
(or even do each byte at a time then put them into the new array)
You need to convert them to integers (no loss, primitive widening), do the XOR, then convert the resulting int back to a byte using a bit mask.
// convert to ints and xor
int one = (int)byte1[0];
int two = (int)byte2[0];
int xor = one ^ two;
// convert back to byte
byte b = (byte)(0xff & xor);
Example
String a = "10101010";
String b = "01010101";
String expected = "11111111"; // expected result of a ^ b
int aInt = Integer.parseInt(a, 2);
int bInt = Integer.parseInt(b, 2);
int xorInt = Integer.parseInt(expected, 2);
byte aByte = (byte)aInt;
byte bByte = (byte)bInt;
byte xorByte = (byte)xorInt;
// conversion routine compacted into single line
byte xor = (byte)(0xff & ((int)aByte) ^ ((int)bByte));
System.out.println(xorInt + " // 11111111 as integer");
System.out.println(xorByte + " // 11111111 as byte");
System.out.println(aInt + " // a as integer");
System.out.println(bInt + " // b as integer");
System.out.println((aInt ^ bInt) + " // a ^ b as integers");
System.out.println(aByte + " // a as byte");
System.out.println(bByte + " // b as byte");
System.out.println(xor + " // a ^ b as bytes");
Prints the following output
255 // 11111111 as integer
-1 // 11111111 as byte
170 // a as integer
85 // b as integer
255 // a ^ b as integers
-86 // a as byte
85 // b as byte
-1 // a ^ b as bytes
You can use the xor operation on bytes. It's the caret (^).
Example:
byte3[0] = (byte) (byte1[0] ^ byte2[0]);
This will work for equal or different size byte array as well.
/** Return XOR of two byte array of different or same size. */
public static byte[] xor(byte[] data1, byte[] data2) {
// make data2 the largest...
if (data1.length > data2.length) {
byte[] tmp = data2;
data2 = data1;
data1 = tmp;
}
for (int i = 0; i < data1.length; i++) {
data2[i] ^= data1[i];
}
return data2;
}
Java has a XOR operator in the form of ^. Just XOR each byte with each subsequent byte and put them in the new array.
I am trying to convert a HEX-sequence to a String encoded in either, ISO-8859-1, UTF-8 or UTF-16BE. That is, I have a String looking like: "0422043504410442" this represents the characters: "Test" in UTF-16BE.
The code I used to convert between the two formats was:
private static String hex2String(String hex, String encoding) throws UnsupportedEncodingException {
char[] hexArray = hex.toCharArray();
int length = hex.length() / 2;
byte[] rawData = new byte[length];
for(int i=0; i<length; i++){
int high = Character.digit(hexArray[i*2], 16);
int low = Character.digit(hexArray[i*2+1], 16);
int value = (high << 4) | low;
if( value > 127)
value -= 256;
rawData[i] = (byte) value;
}
return new String(rawData, encoding);
}
This seems to work fine for me, but I still have two questions regarding this:
Is there any simpler way (preferably without bit-handling) to do this conversion?
How am I to interpret the line: int value = (high << 4) | low;?
I am familiar with the basics of bit-handling, though not at all with the Java syntax. I believe the first part shift all bits to the left by 4 steps. Though the rest I don't understand and why it would be helpful in this certain situation.
I apologize for any confusion in my question, please let me know if I should clarify anything.
Thank you.
//Abeansits
Is there any simpler way (preferably without bit-handling) to do this conversion?
None I would know of - the only simplification seems to parse the whole byte at once rather than parsing digit by digit (e.g. using int value = Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);)
public static byte[] hexToBytes(final String hex) {
final byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
}
return bytes;
}
How am I to interpret the line: int value = (high << 4) | low;?
look at this example for your last two digits (42):
int high = 4; // binary 0100
int low = 2; // binary 0010
int value = (high << 4) | low;
int value = (0100 << 4) | 0010; // shift 4 to left
int value = 01000000 | 0010; // bitwise or
int value = 01000010;
int value = 66; // 01000010 == 0x42 == 66
You can replace the << and | in this case with * and +, but I don't recommend it.
The expression
int value = (high << 4) | low;
is equivalent to
int value = high * 16 + low;
The subtraction of 256 to get a value between -128 and 127 is unnecessary. Simply casting, for example, 128 to a byte will produce the correct result. The lowest 8 bits of the int 128 have the same pattern as the byte -128: 0x80.
I'd write it simply as:
rawData[i] = (byte) ((high << 4) | low);
Is there any simpler way (preferably
without bit-handling) to do this
conversion?
You can use the Hex class in Apache commons, but internally, it will do the same thing, perhaps with minor differences.
How am I to interpret the line: int value = (high << 4) | low;?
This combines two hex digits, each of which represents 4 bits, into one unsigned 8-bit value stored as an int. The next two lines convert this to a signed Java byte.
I'm trying to store a number as a binary string in an array but I need to specify how many bits to store it as.
For example, if I need to store 0 with two bits I need a string "00". Or 1010 with 6 bits so "001010".
Can anyone help?
EDIT: Thanks guys, as I'm rubbish at maths/programming in general I've gone with the simplest solution which was David's. Something like:
binaryString.append(Integer.toBinaryString(binaryNumber));
for(int n=binaryString.length(); n<numberOfBits; n++) {
binaryString.insert(0, "0");
}
It seems to work fine, so unless it's very inefficient I'll go with it.
Use Integer.toBinaryString() then check the string length and prepend it with as many zeros as you need to make your desired length.
Forget about home-made solutions. Use standard BigInteger instead. You can specify number of bits and then use toString(int radix) method to recover what you need (I assume you need radix=2).
EDIT: I would leave bit control to BigInteger. The object will internally resize its bit buffer to fit the new number dimension. Moreover arithmetic operations can be carried out by means of this object (you do not have to implement binary adders/multipliers etc.). Here is a basic example:
package test;
import java.math.BigInteger;
public class TestBigInteger
{
public static void main(String[] args)
{
String value = "1010";
BigInteger bi = new BigInteger(value,2);
// Arithmetic operations
System.out.println("Output: " + bi.toString(2));
bi = bi.add(bi); // 10 + 10
System.out.println("Output: " + bi.toString(2));
bi = bi.multiply(bi); // 20 * 20
System.out.println("Output: " + bi.toString(2));
/*
* Padded to the next event number of bits
*/
System.out.println("Padded Output: " + pad(bi.toString(2), bi.bitLength() + bi.bitLength() % 2));
}
static String pad(String s, int numDigits)
{
StringBuffer sb = new StringBuffer(s);
int numZeros = numDigits - s.length();
while(numZeros-- > 0) {
sb.insert(0, "0");
}
return sb.toString();
}
}
This is a common homework problem. There's a cool loop that you can write that will compute the smallest power of 2 >= your target number n.
Since it's a power of 2, the base 2 logarithm is the number of bits. But the Java math library only offers natural logarithm.
math.log( n ) / math.log(2.0)
is the number of bits.
Even simpler:
String binAddr = Integer.toBinaryString(Integer.parseInt(hexAddr, 16));
String.format("%032", new BigInteger(binAddr));
The idea here is to parse the string back in as a decimal number temporarily (one that just so happens to consist of all 1's and 0's) and then use String.format().
Note that you basically have to use BigInteger, because binary strings quickly overflow Integer and Long resulting in NumberFormatExceptions if you try to use Integer.fromString() or Long.fromString().
Try this:
String binaryString = String.format("%"+Integer.toString(size)+"s",Integer.toBinaryString(19)).replace(" ","0");
where size can be any number the user wants
Here's a simple solution for int values; it should be obvious how to extend it to e.g. byte, etc.
public static String bitString(int i, int len) {
len = Math.min(32, Math.max(len, 1));
char[] cs = new char[len];
for (int j = len - 1, b = 1; 0 <= j; --j, b <<= 1) {
cs[j] = ((i & b) == 0) ? '0' : '1';
}
return new String(cs);
}
Here is the output from a set of sample test cases:
0 1 0 0
0 -1 0 0
0 40 00000000000000000000000000000000 00000000000000000000000000000000
13 1 1 1
13 2 01 01
13 3 101 101
13 4 1101 1101
13 5 01101 01101
-13 1 1 1
-13 2 11 11
-13 3 011 011
-13 4 0011 0011
-13 5 10011 10011
-13 -1 1 1
-13 40 11111111111111111111111111110011 11111111111111111111111111110011
Of course, you're on your own to make the length parameter adequate to represent the entire value.
import java.util.BitSet;
public class StringifyByte {
public static void main(String[] args) {
byte myByte = (byte) 0x00;
int length = 2;
System.out.println("myByte: 0x" + String.valueOf(myByte));
System.out.println("bitString: " + stringifyByte(myByte, length));
myByte = (byte) 0x0a;
length = 6;
System.out.println("myByte: 0x" + String.valueOf(myByte));
System.out.println("bitString: " + stringifyByte(myByte, length));
}
public static String stringifyByte(byte b, int len) {
StringBuffer bitStr = new StringBuffer(len);
BitSet bits = new BitSet(len);
for (int i = 0; i < len; i++)
{
bits.set (i, (b & 1) == 1);
if (bits.get(i)) bitStr.append("1"); else bitStr.append("0");
b >>= 1;
}
return reverseIt(bitStr.toString());
}
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--)
dest.append(source.charAt(i));
return dest.toString();
}
}
Output:
myByte: 0x0
bitString: 00
myByte: 0x10
bitString: 001010
So here instead of 8 you can write your desired length and it will append zeros accordingly. If the length of your mentioned integer exceeds that of the number mentioned then it will not append any zeros
String.format("%08d",1111);
Output:00001111
String.format("%02d",1111);
output:1111