473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

~ operator

Hi,

These 2 lines should be the same. But in IE5, they display 2 different numbers.
Why?

document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");

Thank you.
Jul 20 '05 #1
12 2145
VK
toString() method doesn't take arguments
~ is not a part of number notation

Are you sure you are studying JavaScript and not something else?
If you are not just joking, you may want to look on something like
http://www.devguru.com/Technologies/...ript_intro.htm
l

chirs <ym*@kicon.com> wrote in message
news:4c**************************@posting.google.c om...
Hi,

These 2 lines should be the same. But in IE5, they display 2 different numbers. Why?

document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");

Thank you.

Jul 20 '05 #2
Lee
chirs said:

Hi,

These 2 lines should be the same. But in IE5, they display 2 different numbers.
Why?

document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");


Because numbers are not stored as 32 bit integers.

Jul 20 '05 #3
ym*@kicon.com (chirs) writes:
These 2 lines should be the same.
Says who? :)
But in IE5, they display 2 different numbers. Why?
Because
0xfffffff0 != ~0xf
The value of 0xfffffff0 is 4294967280.
The value of ~0xf = -16.
The comparison is made on Javascript numbers which are not equal.

Javascript integers are not 32 bit numbers, they are actually IEEE
double-precission floating point numbers. The bit-level operations
work by first converting the number to 32-bit integers (skipping
decimals and taking modulo 2^32 on the integer part). The result is then
treated as a *signed* 32-bit two's complement number.

That is why ~0xf = -16. First 15 is converted to a 32-bit number. Then
its bits are inverted, giving the bit-pattern 0xfffffff0. This,
interpreted as a *signed* 32-bit number is -16.

Since you do no bit operations on 0xfffffff0, it is not interpreted as
a signed 32-bit value, just as some positive number.

If you put the ~ on 0xfffffff0 instead, the two lines would give the
same (15). If you interpreted 0xfffffff0 as a 32-bit signed number,
it would also give the same (~~0xfffffff0 == ~0xf).
document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");


(And ~ is the one's complement (bitwise negation) in Javascript, like
in most C-syntax based languages, and Number.prototype.toString takes
an argument in Javascript 1.1+, JScript 2+, and ECMAScript)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
"VK" <sc**********@yahoo.com> wrote in message
news:3f***********************@news.freenet.de...
toString() method doesn't take arguments
<quote ECMA 262 3rd edition>
15.7.4.2 Number.prototype.toString (radix)
If radix is the number 10 or undefined, then this number value
is given as an argument to the ToString operator; the resulting
string value is returned.

If radix is an integer from 2 to 36, but not 10, the result is
a string, the choice of which is implementation-dependent.

The toString function is not generic; it throws a TypeError
exception if its this value is not a Number object. Therefore,
it cannot be transferred to other kinds of objects for use as
a method.
</quote>
~ is not a part of number notation
<quote ECMA 262 3rd edition>
11.4.8 Bitwise NOT Operator ( ~ )
The production UnaryExpression : ~ UnaryExpression is evaluated
as follows:
1. Evaluate UnaryExpression.
2. Call GetValue(Result(1)).
3. Call ToInt32(Result(2)).
4. Apply bitwise complement to Result(3). The result is a
signed 32-bit integer.
5. Return Result(4).
</quote>
Are you sure you are studying JavaScript and not something else?
Looks like JavaScript to me.
If you are not just joking, you may want to look on something like
http://www.devguru.com/Technologies/...ef/javascript_
intro.html


You are recommending that as an accurate and up to date source of
information on JavaScript?

Richard.
Jul 20 '05 #5
Fox


chirs wrote:

Hi,

These 2 lines should be the same. But in IE5, they display 2 different numbers.
Why?

document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");

Bitwise operations are broken in IE...always have been... the sign bit
is not supported.
Thank you.

Jul 20 '05 #6
Fox <fo*@fxmahoney.com> writes:
Bitwise operations are broken in IE...always have been... the sign bit
is not supported.


Can you expand on that? E.g., show something that fails in IE?
As far as I can see, they are working fine in IE6, and I would be
surpriced if even Microsoft could botch something as simple as bitwise
operations.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
VK
Looks like I'm a dinosaur with my 1.x-1.2 JavaScript

I did not realize the amount of changes in later versions.


chirs <ym*@kicon.com> wrote in message
news:4c**************************@posting.google.c om...
Hi,

These 2 lines should be the same. But in IE5, they display 2 different numbers. Why?

document.write((0xfffffff0).toString(10) + "<br>");
document.write((~0xf).toString(10) + "<br>");

Thank you.

Jul 20 '05 #8
"VK" <sc**********@yahoo.com> writes:
Looks like I'm a dinosaur with my 1.x-1.2 JavaScript


Well, the ~ operator was in Javascript 1.0, and the toString method of
numbers with an optional radix existed in Netscape 3 (Javascript
1.1?). :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9
Fox


Lasse Reichstein Nielsen wrote:

"VK" <sc**********@yahoo.com> writes:
Looks like I'm a dinosaur with my 1.x-1.2 JavaScript
Well, the ~ operator was in Javascript 1.0, and the toString method of
numbers with an optional radix existed in Netscape 3 (Javascript
1.1?). :)

yeah -- but it's still better to roll your own -- toString(radix)
(16/hex, 2/binary) insists on prefixing a '-' sign for numbers in which
the MSB is set... it's better to roll your own.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #10
Fox


Lasse Reichstein Nielsen wrote:

Fox <fo*@fxmahoney.com> writes:
Bitwise operations are broken in IE...always have been... the sign bit
is not supported.
Can you expand on that? E.g., show something that fails in IE?
As far as I can see, they are working fine in IE6, and I would be
surpriced if even Microsoft could botch something as simple as bitwise
operations.

[I'm a "Mac-centric" programmer]

Don't be so surprised -- IE is full of these little annoyances [botched?
-- I don't think so -- it serves to derail the competition now that IE
is the majority (modern case in point: the padding/margin box model)].

As long as I can remember, the Mac version of IE has had problems with
the MSB in bitwise operations. Seems to me that there were problems with
earlier pc versions as well. I no longer have versions of IE prior to
IE5.5 for the PC -- maybe someone could check out former versions. I
stopped checking the pc version since as long as there were still
problems with the Mac version, it didn't make sense writing special case
workarounds -- the browser sniff wasted any performance gain that might
have existed using bitwise.

In IE5 (at least on the Mac) bit #31 (MSB) is completely ignored and/or,
incorrect results are returned.

try to set the high bit with |, e.g.: 0x0 | 0x80000000;

IE5 returns 0x7FFFFFF;

as does 0x00000001 | 0x80000000; //same result (in 5.0)! both, of
course, are completely wrong.

(You also cannot test for "negativity" with & 0x80000000.)

Either way, if you program for the general internet population (and
actually take Macs into consideration -- very few pc-only programmers
do) using bitwise operators on the MSB is out of the question and since
IE is no longer being developed for the Mac platform (although it is
still in heavy use) it will *always* have these problems with bitwise operations.

On the plus side, there is little need for bitwise operations in
JavaScript since there is no real speed benefit as there is in compiled
languages. So, instead of testing the "sign" bit with & 0x80000000, it's
just as easy, and perhaps more efficient *in javascript* to simply use a
less than zero test (x < 0). You can still use an integer as a boolean
set, just don't expect to get the use of all 32 bits on a Mac.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #11
Fox <fo*@fxmahoney.com> writes:
yeah -- but it's still better to roll your own -- toString(radix)
(16/hex, 2/binary) insists on prefixing a '-' sign for numbers in which
the MSB is set... it's better to roll your own.


The MSB (most significant bit) doesn't really exist in Javascript
numbers. Javascript numbers are double precission floating point
numbers. The relevant bit is the sign bit, and I can't see any reason
*not* to add a prefix minus if the number is negative.

If that is a problem, run the number through Math.abs first.

Some implementations even allow toString(radix) on non-integer numbers.
In IE6 and Mozilla, (-16.03125).toString(2) == "-10000.00001" . In Opera,
a non-integer argument is converted in base 10. In Netscape 4, the result
is "/0000". I have no idea why.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12
Fox <fo*@fxmahoney.com> writes:
[I'm a "Mac-centric" programmer]

Don't be so surprised -- IE is full of these little annoyances [botched?
-- I don't think so -- it serves to derail the competition now that IE
is the majority (modern case in point: the padding/margin box model)].
Don't get me started :)
But their implementation of ECMAScript has always been one of the better.
I haven't seen any attempts from them to break ECMAScript. They embrace and
extend as usual, giving us JScript, but I would normally trust the core
ECMAScript to be correct.

In IE5 (at least on the Mac) bit #31 (MSB) is completely ignored and/or,
incorrect results are returned.

try to set the high bit with |, e.g.: 0x0 | 0x80000000;

IE5 returns 0x7FFFFFF;
Indeed, that is incorrect. The correct result is -0x80000000 (-2147483648).
I.e., (0x0 | 0x80000000).toString(16) == "-0x80000000" , which is what
IE 6 gives me.
as does 0x00000001 | 0x80000000; //same result (in 5.0)! both, of
course, are completely wrong.
Yes, this should be -0x7FFFFFFF.
(You also cannot test for "negativity" with & 0x80000000.)


You shouldn't be able to.
Both
2147483648 & 0x80000000
and
-2147483648 & 0x80000000
should give the result -2147483648.

Still, inside the range [-2147483648 ... 2147483647], it should work.

It sounds like IE-Mac is indeed broken in this respect.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.