Q:

Write a JavaScript program to reverse the bits of a given 16 bits unsigned short integer

0

Write a JavaScript program to reverse the bits of a given 16 bits unsigned short integer.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function sixteen_bits_reverse(num) {
	var result = 0;
	for (var i = 0; i < 16; i++) 
    {
		result = result * 2 + (num % 2);
		num = Math.floor(num / 2);
	}
	return result;
}
console.log(sixteen_bits_reverse(12345));
console.log(sixteen_bits_reverse(10));
console.log(sixteen_bits_reverse(5));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now