f

#### right-shifting problem

```Hi,

I want to combine 2 different bytes into an integer, so that 1 byte
will be to the left of the second byte.

example:
byte[2] b;
b[0] = 0xE5;
b[1] = 0x35;
I want to get the unsigned number of 0xE535 (=58677), so I did:
int num = (b[0] << 8) | b[1];
But I got the negative number -6859.

How can I get the desired result (58677)?

-thanks, Eli

```
 0
eli.hen (15)
12/18/2005 6:14:46 AM
comp.lang.java.programmer 52711 articles. 1 followers.

3 Replies
268 Views

Similar Articles

[PageSpeed] 56

```Found the solution: ;-)

int num = (((int)b[0] & 0xFF) << 8) | ((int)b[1] & 0xFF)

```
 0
eli.hen (15)
12/18/2005 6:28:23 AM
```On 17 Dec 2005 22:14:46 -0800, eli.hen@gmail.com wrote, quoted or
indirectly quoted someone who said :

>example:
>   byte[2] b;
>   b[0] = 0xE5;
>   b[1] = 0x35;
>I want to get the unsigned number of 0xE535 (=58677), so I did:
>   int num = (b[0] << 8) | b[1];
>But I got the negative number -6859.
>
>How can I get the desired result (58677)?
see http://mindprod.com/jgloss/endian.html
http://mindprod.com/jgloss/unsigned.html
--
http://mindprod.com Java custom programming, consulting and coaching.
```
 0
12/18/2005 11:03:27 AM
```On 17 Dec 2005 22:28:23 -0800, eli.hen@gmail.com wrote, quoted or
indirectly quoted someone who said :

>int num = (((int)b[0] & 0xFF) << 8) | ((int)b[1] & 0xFF)
the int casts are not needed. & 0xff promotes a byte to int.
--