Help with java converting byte to int .... int to byte

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Help with java converting byte to int .... int to byte
by on (#16648)
I made a method to assembler 8 bit into a unique byte.
this follow.
This servers to assembles the 7 flags of cpu.

public static int juntar()
{
int tmp = 0x0 ; //inicializo com 00000000 senão inicializar não aceita de cara o |=

tmp = (
((flagN) ? 128 :0) |
((flagV) ? 64 : 0) |
32 |
((flagB) ? 16 : 0) |
((flagD) ? 8 : 0) |
((flagI) ? 4 : 0) |
((flagZ) ? 2 : 0) |
((flagC) ? 1 : 0)
) ;
return tmp; //I dont know what but if i put byte like a return type this return -78
}

//supossing that i put this value on the variables
byte tmp;
flagN = true ; //10000000
flagV = false; //00000000
//00100000
flagB = true ; //00010000
flagD = false; //00000000
flagI = false; //00000000
flagZ = true ; //00000010
flagC = false; //00000000
//10110010 = 178 decimal or B2 hexa.

Then, this method , if i put the byte type to return it return -78 but if i put int it return 178 , 178 is that correct.
Why this occors?

I need this number, the correct, in byte. And i cannot covert the int to byte?
I try Byte.valueOf(tmp) , but it not works too.
If you can help me I will thanks you !!

by on (#16649)
well.. it is correct.. but java doesn't support unsigned data types.. that's why you get -78 instead of 178.

oh and btw. here is a little read if you're interested:
http://www.darksleep.com/player/JavaAnd ... Types.html

by on (#16650)
dXtr wrote:
well.. it is correct.. but java doesn't support unsigned data types.. that's why you get -78 instead of 178.

oh and btw. here is a little read if you're interested:
http://www.darksleep.com/player/JavaAnd ... Types.html



but if i want the initialize one variable byte with 0;

//isvalid?
byte m = 0;

//how i atribute? this i can but ... this byte m = 129 ; I cannot ?
//rigth?

by on (#16651)
So, if i am writing my emulator on java, i must create all variables like short and not byte.

I need to change array of memory to short too?

by on (#16652)
I think this thread belongs in the newbie forum =)

dreampeppers99 wrote:
So, if i am writing my emulator on java, i must create all variables like short and not byte.

I need to change array of memory to short too?

If you're going to do that, you might as well do it properly and change it to an array of doubles!

...or, you could stick with an array of bytes and just be careful how you handle them, for example, when you read from memory assign the byte to an int and mask it with 0xFF.