Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > 8+8 = 137 ??

Reply
Thread Tools

8+8 = 137 ??

 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-12-2005
"Ingo R. Homann" <(E-Mail Removed)> writes:

> What's the reason, that java does not compile the first one, when it
> really compiles the second one without complaining? Why does java
> treat byte and int so differently in this case?:


It doesn't: The shift operator, like all other operators, promote its
arguments - in this case to int, since neither operand is a double or
long. You then need to mask and/or downcast as needed.
 
Reply With Quote
 
 
 
 
Lee Fesperman
Guest
Posts: n/a
 
      08-12-2005
Tim Tyler wrote:
>
> Lee Fesperman <(E-Mail Removed)> wrote or quoted:
> > Paul Tomblin wrote:
> > > In a previous article, http://www.velocityreviews.com/forums/(E-Mail Removed)lid said:
> > > >On 10 Aug 2005 10:02:30 -0700, "Rajesh.Rapaka"
> > > ><(E-Mail Removed)> wrote or quoted :

>
> > > >>number should be encoded in 16-bit. using java I am reading it as 2
> > > >>eight bits A and B. Now I need to add these 2 bits in such a way that I
> > > >>get the correct value in an 8-bit space.
> > > >
> > > >I presume you mean adding modulo 256.
> > > >
> > > >sum = ( a + b ) & 0xff;
> > >
> > > I think he's trying to put two 8 bit values together to make one 16 bit
> > > value, in which case he'd want
> > >
> > > sum = a << 8 | b;

> >
> > At least that makes sense with his subject, assuming he got his hex to
> > decimal conversion wrong. (8 << 8 | is 0x88 or 136 decimal ... not
> > 137 decimal.

>
> I make it 0x808.


Quite right. I was thinking of:

sum = (a << 4) | b;

((8 << 4) | produces 0x88.

Mea Culpa...

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
================================================== ============
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
 
Reply With Quote
 
 
 
 
Tim Tyler
Guest
Posts: n/a
 
      08-13-2005
Ingo R. Homann <(E-Mail Removed)> wrote or quoted:

> What's the reason, that java does not compile the first one, when it
> really compiles the second one without complaining? Why does java treat
> byte and int so differently in this case?:
>
> byte b = 42;
> b = b << 8; // not OK
>
> int i = 42;
> i = i << 128; // perfectly OK


In the first case Java is promoting to an integer - perhaps because
that's what you might normally want to do - and perhaps so they
don't have to write different bytecode operators for every single
primitive type.

In the second case, Java is not promoting to a 256-bit type -
perhaps because Java doesn't have a 256-bit primitive type - and
perhaps because that's rarely what you would want to do in the first
place.

It doesn't give an overflow error either - Java's primitive integer
types tend not to do that.

Instead it suggests that 42 << 128 is 42.

That is, after all, the answer to everything - isn't it?
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed) Remove lock to reply.
 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      08-15-2005
Hi,

Tim Tyler wrote:
> Instead it suggests that 42 << 128 is 42.


It's getting more and more interesting! Do I understand it correctly,
that when x is an int

x<<y

is really interpreted as

x<<(y%32)

? My test suggest so...

> That is, after all, the answer to everything - isn't it?




Ciao,
Ingo

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      08-15-2005
Ingo R. Homann wrote:

> x<<y
>
> is really interpreted as
>
> x<<(y%32)
>
> ? My test suggest so...


That's right; it's part of the language specification. Similarly, the shift
value for longs is implicitly &-ed with 63.

IMO, the compiler should issue a warning when the 'y' value is a literal value
that it "knows" will be truncated.

-- chris



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange startup connection to 199.239.137.* Sam Firefox 8 07-11-2006 03:50 AM
74-137 =?Utf-8?B?QmVuIEhheQ==?= Microsoft Certification 1 09-06-2005 01:32 AM
Understanding error - Java returned: 137 Manish Hatwalne Java 1 09-03-2004 10:01 AM
Should I disable port 137? zoop Computer Security 4 05-17-2004 07:29 AM



Advertisments