Re: Problem reconstructing decomposed int
On Sun, 28 Jun 2009 15:37:49 -0700, "Mike Schilling"
<mscottschilling@hotmail.com> wrote, quoted or indirectly quoted
someone who said :
// It is irrelevant here whether b1 was sign-extended or not
// "b" will contain the same 0xFE bit pattern regardless, because
// the top 24 bits of the argument get masked off
}
I wrote a little program to prove my point:
import static java.lang.System.out;
public class StudyByte
{
static void display ( byte b )
{
out.println( b );
}
/**
* test harness
*
* @param args not used
*/
public static void main ( String[] args )
{
byte b = (byte) 254;
display ( b );
}
}
Here is how Javap disassembles it.
Compiled from "StudyByte.java"
public class StudyByte extends java.lang.Object
SourceFile: "StudyByte.java"
minor version: 0
major version: 50
Constant pool:
const #1 = Method #6.#17; // java/lang/Object."<init>":()V
const #2 = Field #18.#19; //
java/lang/System.out:Ljava/io/PrintStream;
const #3 = Method #20.#21; //
java/io/PrintStream.println:(I)V
const #4 = Method #5.#22; // StudyByte.display:(B)V
const #5 = class #23; // StudyByte
const #6 = class #24; // java/lang/Object
const #7 = Asciz <init>;
const #8 = Asciz ()V;
const #9 = Asciz Code;
const #10 = Asciz LineNumberTable;
const #11 = Asciz display;
const #12 = Asciz (B)V;
const #13 = Asciz main;
const #14 = Asciz ([Ljava/lang/String;)V;
const #15 = Asciz SourceFile;
const #16 = Asciz StudyByte.java;
const #17 = NameAndType #7:#8;// "<init>":()V
const #18 = class #25; // java/lang/System
const #19 = NameAndType #26:#27;// out:Ljava/io/PrintStream;
const #20 = class #28; // java/io/PrintStream
const #21 = NameAndType #29:#30;// println:(I)V
const #22 = NameAndType #11:#12;// display:(B)V
const #23 = Asciz StudyByte;
const #24 = Asciz java/lang/Object;
const #25 = Asciz java/lang/System;
const #26 = Asciz out;
const #27 = Asciz Ljava/io/PrintStream;;
const #28 = Asciz java/io/PrintStream;
const #29 = Asciz println;
const #30 = Asciz (I)V;
{
public StudyByte();
Signature: ()V
LineNumberTable:
line 3: 0
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
static void display(byte);
Signature: (B)V
LineNumberTable:
line 7: 0
line 8: 7
Code:
Stack=2, Locals=1, Args_size=1
0: getstatic #2; //Field
java/lang/System.out:Ljava/io/PrintStream;
3: iload_0
4: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
7: return
LineNumberTable:
line 7: 0
line 8: 7
public static void main(java.lang.String[]);
Signature: ([Ljava/lang/String;)V
LineNumberTable:
line 17: 0
line 18: 3
line 19: 7
Code:
Stack=1, Locals=2, Args_size=1
0: bipush -2 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2: istore_1
3: iload_1
4: invokestatic #4; //Method display:(B)V
7: return
LineNumberTable:
line 17: 0
line 18: 3
line 19: 7
}
The key is bipush -2. It is pushing a 32-bit sign extended -2 onto the
the stack, not 0xfe as you thought. bytes are treated like signed
32-bit ints inside the JVM.
--
Roedy Green Canadian Mind Products
http://mindprod.com
"Deer hunting would be fine sport, if only the deer had guns."
~ William S. Gilbert of Gilbert and Sullivan