unisgned vs. signed values

A

Aryeh.Friedman

Please don't flame me if this is a FAQ. How do I force
byte/short/int/long/etc. to be unisgned (i.e. the signbit is treated as
2^n instead of signbit-1 being 2^n). The specific application I have
is using a calculated byte value as an array index and due
to the signbit if MSB is set then it is always negative vs. being
between 128 and 255.
 
B

Bob

Please don't flame me if this is a FAQ. How do I force
byte/short/int/long/etc. to be unisgned (i.e. the signbit is treated as
2^n instead of signbit-1 being 2^n). The specific application I have
is using a calculated byte value as an array index and due
to the signbit if MSB is set then it is always negative vs. being
between 128 and 255.

Hold a sec while I just insert a new butane cartridge...

The answer, as far as my useless memory tells me, is that you don't.

All numeric primitives in Java are signed.

Have you any code you can show us to explain what you're trying to do?
 
P

Patricia Shanahan

An array can't have more than Integer.MAX_VALUE elements, so
this only applies to the signed integer types that are
narrower than int. Although int and long are both signed,
all valid array indices fit in their non-negative ranges.

There are several ways of doing this. The one I like best is
to simply cast to int, and then turn off the sign extension
bits:

public class UByteToInt {

public static int uByteToInt(byte in){
return (int)in & 0xff;
}

public static void main(String[] args) {
for(int i=-128; i<=127; i++){
byte b = (byte)i;
System.out.println(b+ " " + uByteToInt(b));
}
}
}

That said, avoid this if possible. Why use byte, when int
does the job better?

Hold a sec while I just insert a new butane cartridge...

The answer, as far as my useless memory tells me, is that you don't.

All numeric primitives in Java are signed.

What about char? It's numeric and unsigned.
 
A

Aryeh.Friedman

Patricia said:
An array can't have more than Integer.MAX_VALUE elements, so
this only applies to the signed integer types that are
narrower than int. Although int and long are both signed,
all valid array indices fit in their non-negative ranges.

Thats what I figured orginally.
There are several ways of doing this. The one I like best is
to simply cast to int, and then turn off the sign extension
bits:

That was the solution I wanted to avoid ;-)
public class UByteToInt {

public static int uByteToInt(byte in){
return (int)in & 0xff;
}

That said, avoid this if possible. Why use byte, when int
does the job better?

Since the way the index is derived is by slicing a int/long into it's
component bytes then using those as a virtual multi-dimensional array
subscript (tree path actually). Namely if the int is 0xffddeecc then
then the path taken is ff->dd->ee->cc. Now where the issue comes from
is even though the int/long may not be signed the indivual bytes maybe
What about char? It's numeric and unsigned.

Ok will try a char since it sounds like the right think to convert
everything to
(i.e. the byte extractor stores the slice as a char not a byte).

Not that I can legally post (any example I can think of would break my
NDA).

--Aryeh
 
P

Patricia Shanahan

Since the way the index is derived is by slicing a int/long into it's
component bytes then using those as a virtual multi-dimensional array
subscript (tree path actually). Namely if the int is 0xffddeecc then
then the path taken is ff->dd->ee->cc. Now where the issue comes from
is even though the int/long may not be signed the indivual bytes maybe
and when you convert a neg byte to an int it gives some <bleeping>
strange results.

The code I gave you will work perfectly for that
application, if applied to each byte after extraction from
the int.

Alternatively, you could do it directly by shifting and
masking, without ever going to byte. To extract each
component, shift right by the number of bits that are less
significant then the component (24, 16, 8, 0) and bitwise &
with 0xff. No need to go to bytes at all.

Patricia
 
A

Aryeh.Friedman

Here's the byte extractor as currently written:

If the byte (as calculated by the return statement of getByte) is
negative then the resulting long is <bleep>'ed up.

public class GetByte {
public static long getByte(long i,long pos)
{
// pos must be long to get javac to shutup

if(Masks[0]==0)
setMasks();

return (i&Masks[(int) pos])>>((long) pos*8);
}

public static byte [] intToBytes(int in)
{
byte [] b=new byte[4];

// assume 32 bit ints
for(int i=0;i<4;i++)
b=(byte) getByte(in,i);

return b;
}

public static byte [] longToBytes(long in)
{
byte [] b=new byte[8];

// assume 32 bit ints
for(int i=0;i<4;i++)
b=(byte) getByte(in,i);

return b;
}

public static byte [] longToBytes(long in)
{
byte [] b=new byte[8];

// assume 64 bit longs
for(int i=0;i<8;i++)
b=(byte) getByte(in,i);

return b;
}

private static void setMasks()
{
for(int i=0;i<8;i++)
Masks=255<<(i*8);
}

static long [] Masks={0,0,0,0,0,0,0,0};
}
 
P

Patricia Shanahan

Here's the byte extractor as currently written:

If the byte (as calculated by the return statement of getByte) is
negative then the resulting long is <bleep>'ed up.

public class GetByte {
public static long getByte(long i,long pos)
{
// pos must be long to get javac to shutup

if(Masks[0]==0)
setMasks();

return (i&Masks[(int) pos])>>((long) pos*8);
}

You are making it far more complicated than it needs to be.

You must either mask AFTER all extraction and shifting, as I
did, or use >>>, instead of >>, to get an unsigned shift. It
is much simpler to mask after shifting, because that way you
only have one mask, 0xff.

Patricia
 
A

Aryeh.Friedman

I did almost that.... between my last post and your reply I adeed
(....)&oxff to the getByte() return, works fine.

--Aryeh
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top