Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #24762

Re: "i = i|0"

From raltbos@xs4all.nl (Richard Bos)
Newsgroups comp.lang.javascript
Subject Re: "i = i|0"
Date 2014-06-12 11:33 +0000
Organization Go wash your mouth.
Message-ID <53998e77.9430437@news.xs4all.nl> (permalink)
References <emscripten-20140611221102@ram.dialup.fu-berlin.de> <5051886.MyCAqxjJIy@PointedEars.de>

Show all headers | View raw


Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:

> [F'up2 comp.lang.javascript]
> 
> Stefan Ram wrote:
> 
> >   I have read this in a World-Wide Web encyclopedia:
> > 
> > For example, given the following C code:
> > 
> > int f(int i) {
> >   return i + 1;
> > }
> > 
> > Emscripten would output the following JS code:
> > 
> > function f(i) {
> >   i = i|0;
> >   return (i + 1)|0;
> > }
> > 
> >   Do you think that the »|0« is necessary to express the
> >   C semantics in JavaScript, or could the speed of the
> >   generated code be improved by omitting it?
> 
> First of all, this does _not_ express the C semantics in JavaScript, and 
> there is no other way.  “int” is a *generic* type in C/C++; IIUC, the result 
> could be a 32-bit integer when compiled for a 32-bit platform or a 64-bit 
> integer when compiled for a 64-bit platform.

Please don't talk about things you understand nothing of. There is no
such things as C/C++; and to describe int as "generic" is overstating
the matters. int is the _natural_ type in a C implementation, within
certain limits.

> <http://en.wikibooks.org/wiki/C_Programming/Reference_Tables#Table_of_Data_Types>
> <http://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes>
> 
> By contrast, using the binary bitwise OR operator, as with all ECMAScript 
> binary bitwise operators, *always* creates an IEEE-754 double-precision 
> *floating-point* value representing a *32-bit* integer value.

Which is about the most broken way you could go about using a bitwise OR
operator (OK, you could make it more broken by choosing a smaller FP
representation, but...); but this at least you're correct on, it's not
the same things as the C bitwise OR operator nor as the normal C integer
constraint rules (which, BTW, allow for unexpected (no, more unexpected
than you're now expecting) results when the +1 overflows).

Richard

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: "i = i|0" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-12 00:20 +0200
  Re: "i = i|0" raltbos@xs4all.nl (Richard Bos) - 2014-06-12 11:33 +0000
    Re: "i = i|0" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-12 14:28 +0200
  Re: "i = i|0" Thomas Richter <thor@math.tu-berlin.de> - 2014-06-12 19:52 +0200
    Re: "i = i|0" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-12 20:25 +0200

csiph-web