সমস্যা
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা একটি দশমিক সংখ্যা নেয়, এটিকে বাইনারিতে রূপান্তর করে এবং এর 1 বিটকে 0 এবং 0 থেকে 1 তে বিপরীত করে এবং এইভাবে গঠিত নতুন বাইনারিটির দশমিক সমতুল্য ফেরত দেয়।
উদাহরণ
নিম্নলিখিত কোড -
const num = 45657;
const reverseBitsAndConvert = (num = 1) => {
const binary = num.toString(2);
let newBinary = '';
for(let i = 0; i < binary.length; i++){
const bit = binary[i];
newBinary += bit === '1' ? '0' : 1;
};
const decimal = parseInt(newBinary, 2);
return decimal;
};
console.log(reverseBitsAndConvert(num)); আউটপুট
19878