Georgiana.b
Georgiana.b

Reputation: 359

Comparing numbers digit by digit

I want to write a function that compares the numbers in an array and deletes those who contain the same digits (ex. 1335 531) .The deleting part arouses no problems but I can't seem to figure out how to compare them digit by digit, especially when they don't have the same length. Any idea is more that welcomed and appreciated.

Upvotes: 1

Views: 1156

Answers (1)

Mooing Duck
Mooing Duck

Reputation: 66922

 unsigned get_digit_mask(unsigned input) 
 {
     unsigned result = 0;
     unsigned digit;
     do {
         digit = input%10; //get the rightmost digit
         input/=10; //remove it from the number
         result |= (1<<digit); //set that bit of the result
     }while(input); //continue as long as there's more digits
     return result; //return bitmask of used digits
 }

If you use this function on the number 1335, it will return a mask with the 1st, 3rd, and 5th bits set. If you give the function the number 531, it will return a mask with the 1st, 3rd, and 5th bits set. If the masks are equal, then the numbers contain the same digits.

Upvotes: 3

Related Questions