wierd cast from int to byte

D

djbitchpimp

I casted an 8 ints to byte types. Some of them come out in the right
format, but when I print them, some come out way bigger than 8 bits.

Here is the string representation of the byte [8] array:

1111111111111111111111110000101 1111111111111111111111111101000 10011
1010100 1111 1010 1111111111111111111111110110100 101

How can I truncate off the extra 1's to just get the last 8 bits of
values [0], [1], and [6]?
 
T

Thomas Hawtin

I casted an 8 ints to byte types. Some of them come out in the right
format, but when I print them, some come out way bigger than 8 bits.

Here is the string representation of the byte [8] array:

1111111111111111111111110000101 1111111111111111111111111101000 10011
1010100 1111 1010 1111111111111111111111110110100 101

How can I truncate off the extra 1's to just get the last 8 bits of
values [0], [1], and [6]?

All numerical types are signed (except char). Before any arithmetic on
byte, char or short is performed the value is transformed into an int.

Consider the byte -1, which is 11111111 in binary. If you wanted to see
if bit n of byte b is set you might try (b & (1<<n)) != 0. If b is
11111111 (binary) then it is first converted to the int
11111111111111111111111111111111 (binary). So, you need to either remove
or ignore the top 24 bits.

You can remove all but the bottom eight bits of an int, i, with i &
0xff. So to get an unsigned binary representation of a byte, b, you can
use Integer.toBinaryString(b & 0xff).

Tom Hawtin
 
D

djbitchpimp

byte encrypted [] = new byte [8] ;
int encryptedInt [] = new int [] = {
1,0,0,0,0,1,0,1,1,1,1,0,1,0,0,0,0,0,0,1,0,0,1,1,
0,1,0,1,0,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,0,1,01,0,1,1,0,1,0,0,
0,0,0,0,0,1,0,1 };

// convert encryptedInt back to binary
int pack [] = new int [8] ;

for (i = 0; i < 8; i++) { //each individual byte
for (int j = 0; j < 8; j++) { // each bit in that byte
pack [j] = encryptedInt [j+(8*i)] ;
}
encrypted = packByte (pack) ;
}

private byte packByte (int [] toPack) {
int byteVal = 0 ;
int temp = 0 ;
int i ;

byteVal = toPack[0] & 0x1 ;
byteVal <<= 7 ;
for (i = 7; i > 0; i--) {

temp = toPack [8 - i] & 0x1 ;
//System.out.print ("Bit " + (8 - i) + " " + temp + " ") ;
temp <<= i - 1 ;
//System.out.println (temp) ;
byteVal = byteVal | temp ;
byteVal = byteVal & 0xff ;
//System.out.println (byteVal) ;
}

// byteval has the value of the byte
return (byte) byteVal ;

}
 
D

djbitchpimp

I just posted some code in reply to Thomas. When I print out the value
of the byte [] array in binary:

1111111111111111111111110000101 1111111111111111111111111101000 10011
1010100 1111 1010 1111111111111111111111110110100 101
 
D

djbitchpimp

byteArrToString(encrypted) ;

/* prints out the binary representation of a byte array */
public static void byteArrToString (byte [] toPrint) {
for (int i = 0; i < toPrint.length; i++) {
printBinary(toPrint ) ;
}

//System.out.println () ;
}

// Takes an int as an argument and prints it in binary.
// Returns the length of the number in binary.
// if boolean is false, it won't print anything.

public static void printBinary(byte value) {
boolean shouldPrint = true;
int hasPrinted = 0;
int pos = 32;
while (pos > 0) {
if (checkBit(value, pos)) {
System.out.print(shouldPrint ? "1" : "");
hasPrinted++;
} else if (hasPrinted > 0) {
System.out.print(shouldPrint ? "0" : "");
hasPrinted++;
}
pos--;
}
System.out.print(shouldPrint ? " " : "");
//return hasPrinted;
}
 
S

Steve Horsley

byteArrToString(encrypted) ;

/* prints out the binary representation of a byte array */
public static void byteArrToString (byte [] toPrint) {
for (int i = 0; i < toPrint.length; i++) {
printBinary(toPrint ) ;
}

//System.out.println () ;
}

// Takes an int as an argument and prints it in binary.
// Returns the length of the number in binary.
// if boolean is false, it won't print anything.

public static void printBinary(byte value) {
boolean shouldPrint = true;
int hasPrinted = 0;
int pos = 32;
while (pos > 0) {
if (checkBit(value, pos)) {
System.out.print(shouldPrint ? "1" : "");
hasPrinted++;
} else if (hasPrinted > 0) {
System.out.print(shouldPrint ? "0" : "");
hasPrinted++;
}
pos--;
}
System.out.print(shouldPrint ? " " : "");
//return hasPrinted;
}


You don't post the code for checkBit(byte), but I guess that your
problem is actually this line:
int pos = 32;
This explicitly asks to print bit values from position 32
downwards. Starting with pos=8 shoudl fix it.

You seem not to know about promotion and sign extension. Almost
any operation on a byte (including bitwise operations) first
promotes the byte to an int value and then performs the
operation. As part of that promotion, a negative byte (e.g. -1 =
11111111) has the extra ones added to keep the same value as an
int (-1 = 11111111111111111111111111111111).

HTH
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top