Converting int to byte

K

Konrad Viltersten

For converting from int to byte i use the following method.

private int getIntFromByte (byte b) {
if (b >= 0) return (int)b;
return (int)(256 + b); }

I have two questions regarding that.
1. Is there a shorter way to express it (i think i saw some
operators like "<" or "&" or something like that in C, at
least)? Something corresponding to the (a==b)?"a":"b"
as a shorthand for if/else regarding strings...
2. How (timely) effective is the method i use compared
to any alternative (if any can be suggested)?

--

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy

---------------------------------------------------
 
S

Skip

Konrad Viltersten said:
For converting from int to byte i use the following method.

private int getIntFromByte (byte b) {
if (b >= 0) return (int)b;
return (int)(256 + b); }

Fastest would be:
return b & 0xFF;

0xFF equals 255 equals 0b11111111

So in fast you "copy" the 8 bits of the byte into the int. For bytes the 8th
bit is the sign-bit, which will be just copied to the int, instead of
converted to an int using the 32th bit as a sign-bit.

http://mindprod.com/jgloss/twoscomplement.html

HTH
 
O

Owen Jacobson

For converting from int to byte i use the following method.

private int getIntFromByte (byte b) {
if (b >= 0) return (int)b;
return (int)(256 + b); }

I have two questions regarding that.
1. Is there a shorter way to express it (i think i saw some
operators like "<" or "&" or something like that in C, at
least)? Something corresponding to the (a==b)?"a":"b"
as a shorthand for if/else regarding strings...
2. How (timely) effective is the method i use compared
to any alternative (if any can be suggested)?

It looks like you actually want to convert the representation of the byte
to a corresponding int, without sign extension, as the normal conversion
from byte to int is simply to assign the byte value to an int variable.

I *think* what you want is

private int getIntFromByte (byte value) {
return value & 0xFF;
}

(`value` is automatically promoted to an int, with the same value, before
the & operator is applied; the bitwise-AND then zeroes all but the first
eight bits, cancelling out the sign-extension for negative numbers.)

-Owen
 
K

Konrad Viltersten

private int getIntFromByte (byte b) {
Fastest would be:
return b & 0xFF;

0xFF equals 255 equals 0b11111111
http://mindprod.com/jgloss/twoscomplement.html


Thanks to both of you. I'll give it a try right away.

--

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy

---------------------------------------------------
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top