bitwise operators - javascript inverting a binary value of a number -


i first convert number binary, invert bitwise .. :

number 793 = 1100011001 convert binary value : 0011100110

in javascript can following :

var x = 793; document.write(x.tostring(2)); // gives 0011100110 

this give me binary value of number .. how invert binary bitwise ?

i tried ~ operator, not working probably... output : -1100011010

any ? in advance

moogoo's answer correct.

here information happening.... lets assume 64 bit integer.

793 = 1100011001 ~793 = -794 = 1111111111111111111111111111111111111111111111111111110011100110 0x3ff = 1111111111 (-793 & 0x3ff) = 11100110 

so solve cases code:

var x = 793; // input value var y = x.tostring(2); var yl = y.length; var mask = (math.pow(2,yl)-1); // calculate mask var result = ~x & mask; document.write(result.tostring(2)+"<br/>"); 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -