<html> <head> <title>JavaScript Unleashed</title> </head> <body> <script type="text/javascript"> <!-- function getBinary(anInteger) { var result = ""; var shortResult = ""; for(var i=1; i <= 32; i++) { if(anInteger & 1 == 1) { result = "1" + result; shortResult = result; } else { result = "0" + result; } anInteger = anInteger >> 1; } return(shortResult); }
var binaryString = "";
x = 127; binaryString = getBinary(x);
document.write("The number " + x + " in binary form is : n"); document.writeln(binaryString);
x = 255; binaryString = getBinary(x);
document.write("The number " + x + " in binary form is : n"); document.writeln(binaryString); document.writeln("The variable x is still equal to : " + x); // --> </script>
</body> </html>
Related Scripts with Example Source Code in same category :