chanma wrote:

code1:var x=0xf0000000;

alert(x);

output:4026531840

code2:var b=0xf<<28;

alert(b);

output:-268435456

I have such above codes , how can code2

output the same as code1?!

A possibly unexpected characteristic of javascript is that there is no

such thing as a negative numeric literal in the language. When a numeric

literal is preceded by a minus sign that minus sign is actually a unary

minus operator, with the (exclusively positive) numeric literal as its

operand. This is as true for hexadecimal literals as it is for decimal

literals.

Javascript's single numeric type is an IEEE 754 64 bit double precision

floating point number. some operations, particularly bitwise operations,

convert the javascript floating point number type into (32 bit and 16

bit) integer types for internal evaluation. The output of these

operations can always be precisely represented by the 64 bit double

precision number type (its mantissa is longer than 32 bits and its

exponent longer than 5), however, the process of converting a floating

point number to an integer will modify many numeric values. All

fractional parts must be truncated, and numbers with values outside the

range that the internal 32/16 bit type is capable of representing must

be modified.

The language specification defines three operations/function that are

used to convert the floating point number type into internal integer

representations. They are:- ToInt36 (ECMA 262, 3rd ed. Section 9.5),

ToUint32 (Section 9.6) and ToUint16 (section 9.7). ToUint16 is

irrelevant to your situation.

Your - x = 0xf0000000 - produces a positive numeric value by virtue of

the fact that all numeric literals are positive, and no unary minus

operation is applied to the value of the numeric literal. Your -

b=0xf<<28 - produces a negative result because the left shit (<<)

operation uses signed 32 bit integers internally. That is, it applies

the internal ToInt32 function to its left hand side operand, shifts that

value (which becomes negative because the highest bit is then set) and

then converts that value into a double precision number (which is

negative as it has the same numeric value as the internally used signed

32 bit value).

One of the bitwise operators uses an unsigned 32 value internally (as

produced with the ToUint23 function). That operator is the unsigned

right shift operator (>>>).

This means that you can convert a value that can be precisely

represented as a signed 32 bit integer into an equivalent of an unsigned

32 bit integer where the unsigned 32 bit integer would have the same bit

pattern as the signed 32 bit integer by applying - x >>> 0 - (unsigned

right shift by zero bits). I.E:-

code2: var b = (0xf << 28) >>> 0;

alert(b);

output: 4026531840

But remember that the 32 bit integers employed in javascript are

internal intermediate values. The exposed numeric values are all 64 bit

double precision floating point numbers. They can represent the same

values as 32 bit integers but those values are held in very different

bit patterns.

Richard.