
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Checking if decimals share at least two common 1 bits in JavaScript
Problem
We are required to write a JavaScript function that takes in two numbers. Our function should return true if the numbers have 1 in the binary representation at the same index twice, false otherwise.
Example
Following is the code −
const num1 = 10; const num2 = 15; const checkBits = (num1 = 1, num2 = 1) => { let c = num1.toString(2).split(''); let d = num2.toString(2).split(''); if(c.length > d.length){ c = c.slice(c.length - d.length); }else{ d = d.slice(d.length - c.length); }; let count = 0; for(let i = 0; i < d.length; i++){ if(c[i] === "1" && d[i] === "1"){ count++; }; }; return count > 1; }; console.log(checkBits(num1, num2));
Output
Following is the console output −
true
- Related Articles
- Python program to check if two lists have at least one common element
- C# program to check if two lists have at-least one element common
- Subarray sum with at least two elements in JavaScript
- Design a DFA of a string with at least two 0’s and at least two 1’s
- Function to calculate the least common multiple of two numbers in JavaScript
- Checking if two arrays can form a sequence - JavaScript
- Common element with least index sum in JavaScript
- How to share private members among common instances in JavaScript?
- Calculating least common of a range JavaScript
- Write a program to calculate the least common multiple of two numbers JavaScript
- How to aggregate two lists if at least one element matches in MongoDB?
- Make the sample space of tossing two coins simultaneously and find the probability of:1. Exactly two heads2. At least one head3. At least one tail4. At most two tails
- Check if SortedSet and a specified collection share common elements in C#
- Checking if change can be provided in JavaScript
- Check if a HashSet and a specified collection share common element in C#

Advertisements