By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

How to ignore the signed bit?

P: n/a
I have an num -569360386, and turn it into hex format.
I use toString(16),I get -21efc002.

But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?

Jan 22 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On 22 Jan 2006 02:47:08 -0800, ch****@163.com wrote, quoted or
indirectly quoted someone who said :
I have an num -569360386, and turn it into hex format.
I use toString(16),I get -21efc002.

But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?

see http://mindprod.com/jgloss/hex.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Jan 22 '06 #2

P: n/a
Roedy Green wrote on 22 jan 2006 in comp.lang.javascript:
On 22 Jan 2006 02:47:08 -0800, ch****@163.com wrote, quoted or
indirectly quoted someone who said :
I have an num -569360386, and turn it into hex format.
I use toString(16),I get -21efc002.

But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?
see http://mindprod.com/jgloss/hex.html


That is about Java, not Javascript!

-21efc002 seems correct to me

==========================

n = -569360386

document.write(n.toString(16)+'<br>'); // -21efc002.

n = 569360386

document.write(n.toString(16)); // 21efc002.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 22 '06 #3

P: n/a
Then is there any way to Get 0xDE103FFE but not -21efc002???

Must I use my own code to get translate it?

Jan 22 '06 #4

P: n/a
wrote on 22 jan 2006 in comp.lang.javascript:
Then is there any way to Get 0xDE103FFE but not -21efc002???

Please quote what you are replying to.

This is not email.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at the
top of the article, then click on the "Reply" at the bottom of the article
headers.
Must I use my own code to get translate it?


0xDE103FFE is not an output in javascript

The solution seems simple as each figure is the 16-complement:
n = '-21efc002';

n = n.toUpperCase();
if (n.charAt(0)=='-'){
r = 'FEDBCA9876543210';
m = '';
for (i=1;i<n.length;i++){
g = n.charCodeAt(i)-48;
if (g>9)g=g-7;
m += r.charAt(g);
};
n = m;
};
n = '0x' + n

document.write(n); // 0xDE103FFD

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 22 '06 #5

P: n/a
On Sun, 22 Jan 2006 11:04:14 GMT, Roedy Green
<my******************************@munged.invalid > wrote, quoted or
indirectly quoted someone who said :
But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?

see http://mindprod.com/jgloss/hex.html


sorry. That's the java answer to your question.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Jan 22 '06 #6

P: n/a
Evertjan. wrote on 22 jan 2006 in comp.lang.javascript:
0xDE103FFE is not an output in javascript

The solution seems simple as each figure is the 16-complement:
n = '-21efc002';

n = n.toUpperCase();
if (n.charAt(0)=='-'){
r = 'FEDBCA9876543210';
m = '';
for (i=1;i<n.length;i++){
g = n.charCodeAt(i)-48;
if (g>9)g=g-7;
m += r.charAt(g);
};
n = m;
};
n = '0x' + n

document.write(n); // 0xDE103FFD


I leave the obvious difference of 1 to your coding ability.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 22 '06 #7

P: n/a

Evertjan. wrote:
Evertjan. wrote on 22 jan 2006 in comp.lang.javascript:
0xDE103FFE is not an output in javascript


But in such code below:

var x=0xf0000000;
alert(x);

it can output :4026531840 !

So how the JS engine interprets it as unsigend numbers??

Jan 22 '06 #8

P: n/a
ch****@163.com writes:
I have an num -569360386, and turn it into hex format.
I use toString(16),I get -21efc002.

But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?


Since numbers in Javascript are 64-bit IEEE floating point numbers,
the "highest bit" probably isn't what you think it is.
You can convert -12312151251245125125 to hex as well.

What you might want it to treat the number as a 32 bit integer.
Luckily, that is an internal datatype in Javascript, and some
operations (in particular bitwise ones) does convert/truncate the
number to a 32 bit integer before operating on it. However, that
number can still be signed.

try:
function myToHex(num) {
num <<= 0; // truncate to 32 bits;
if (num < 0) { num = num ^ (1<<31); }
return "0x" + num.toString(16);
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 22 '06 #9

P: n/a
ch****@163.com escreveu:
Then is there any way to Get 0xDE103FFE but not -21efc002???
Must I use my own code to get translate it?


Since
0xDE103FFE = 3725606910 = 11011110000100000011111111111110
And
~0xDE103FFE + 1 = 569360386 = 100001111011111100000000000010

A xor operation (569360386 ^ 0xffffffff) + 1 would solve your problem:
11111111111111111111111111111111 [XOR]
00100001111011111100000000000010
--------------------------------------------------------
11011110000100000011111111111101 [+ 1] =
11011110000100000011111111111110

But due to the range of numbers available to binary operations, it
won't work :)

So you must make a work around like this one:

x = 569360386;
alert(((0x7fffffff ^ x) + 0x80000001).toString(16));

I've tested it just with your example number (569360386), so test a
little for me haha =)
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com

Jan 22 '06 #10

P: n/a
JRS: In article <11**********************@g14g2000cwa.googlegroups .com>
, dated Sun, 22 Jan 2006 02:47:08 remote, seen in
news:comp.lang.javascript, ch****@163.com posted :
I have an num -569360386, and turn it into hex format.
I use toString(16),I get -21efc002.

But how can get 0xDE103FFE,which is to ignore the highest bit as the
signed bit?


"0x" + (Math.pow(2, 32) - 569360386).toString(16).toUpperCase()

To accommodate the widest possible range, you may need different code
for positive and negative, or to add leading zeroes, or to form

"0x" + (Math.pow(2, 52) - 569360386).toString(16).toUpperCase()

and select the required number of digits.

If done repeatedly, the power of 2 should be written as a constant :

"0x" + (0x10000000000000 - 569360386).toString(16).toUpperCase()

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 22 '06 #11

P: n/a
I think following code mach easy :)

function toHexString(_intValue)
{
var intHigh = (_intValue >> 16) & 0xFFFF;
return "0x" + intHigh.toString(16) + (_intValue &
0xFFFF).toString(16);
}

Jan 23 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.