casting int to byte

R

Rob Shepherd

when applying (byte)bit masks to bytes, do i always have to cast my mask in to a byte.

[just realizing i've even had to do it in previous sentence :) ]

javac output is thus.

....
operator && cannot be applied to byte,int
length += ((packet[13] && 0x0f) << 4);
....

does the compiler assume 0x0f is an int as it would if a long or float value were not
suffixed L or F respectively.

I gather i will not be able to add a 'b' or 'B' to the end of my value (being Hex digits).

I'm getting annoyed with casting all the time!!!

For freq used masks i make

static final byte MASK = 0x??;

which the compiler obviously has no problems with....

I'd rather not make 255 final byte masks though!!!

regards

Rob
 
K

Kristoffel

Rob said:
when applying (byte)bit masks to bytes, do i always have to cast my mask
in to a byte.

[just realizing i've even had to do it in previous sentence :) ]

javac output is thus.

...
operator && cannot be applied to byte,int
length += ((packet[13] && 0x0f) << 4);

(packet[13] && 0x0f) results in a boolean !

I assume that it should be
length += ((packet[13] & 0X0F) << 4);

with one '&'
 
R

Rob Shepherd

Kristoffel said:
Rob said:
when applying (byte)bit masks to bytes, do i always have to cast my
mask in to a byte.

[just realizing i've even had to do it in previous sentence :) ]

javac output is thus.

...
operator && cannot be applied to byte,int
length += ((packet[13] && 0x0f) << 4);


(packet[13] && 0x0f) results in a boolean !

I assume that it should be
length += ((packet[13] & 0X0F) << 4);

with one '&'

...

does the compiler assume 0x0f is an int as it would if a long or float
value were not suffixed L or F respectively.

I gather i will not be able to add a 'b' or 'B' to the end of my value
(being Hex digits).

I'm getting annoyed with casting all the time!!!

For freq used masks i make

static final byte MASK = 0x??;

which the compiler obviously has no problems with....

I'd rather not make 255 final byte masks though!!!

regards

Rob


Yeah... my bad!! i'm having a confusing afternoon!! too much bit-banging :)
please ignore!!

thanks Kristoffel.


Ro
 
R

Roedy Green

when applying (byte)bit masks to bytes, do i always have to cast my mask in to a byte.

[just realizing i've even had to do it in previous sentence :) ]

If you cast them to bytes, you are just truncating the high order
bits. The operators such as & + | all work on ints, and any bytes will
be automatically widened back to ints. Unfortunately bytes are signed,
so automatic widening will just put high order bits back on again.

Use & 0xff instead when you want unsigned bytes.


See http://mindprod.com/jgloss/unsigned.html
 
R

Roedy Green

(packet[13] && 0x0f) results in a boolean !

No, it result in an syntax error. && will only operate on booleans
in Java.

You want &, the bitwise operator.
 
L

Liz

Roedy Green said:
when applying (byte)bit masks to bytes, do i always have to cast my mask in to a byte.

[just realizing i've even had to do it in previous sentence :) ]

If you cast them to bytes, you are just truncating the high order

You can't truncate the high order bits :)
trun·cate
trun·cate (trùng¹kât´) verb, transitive
trun·cat·ed, trun·cat·ing, trun·cates
1. To shorten................
2. To shorten (a number) by dropping one or more digits after the
decimal point.
3. To replace................
 
R

Roedy Green

You can't truncate the high order bits :)
trun·cate
1. To shorten................

You are shortening the "payload". I suppose I could have said
zero-out or mask-off the high order bits.
 

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,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top