Opposites Attract, C, CodeWars

Problem Statement

Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.

Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren't.

Chosen Solution

My chosen solution for this problem is to go with XOR. Since we are comparing odd/even. Under the properties of integers, we know

Even + Even = Even
Odd + Odd = Even
Even + Odd = Odd

Therefore, the only condition that satisfies for Timmy & Sarah to be in love would be for the total to be Odd. Knowing this, we can forget about the inputs and compare totals. I will show this in a later solution. However, for my final solution I have chosen a much more compact, and simple method of going about this. Using XOR, we can compare the last bits of a number translated into binary (base 2) as so.

6 --> 0110
3 --> 0011

Just like in base 10, how we can find out if a number is odd or even based on the last digit's divisibility by 10, we can do the same with the last digits here with divisibility by 2. In other words, if the last bit is 0, the number is made of only 2, 4, 8, etc. --> it's divisible by 2 --> even. If the last bit is 1, the number is 1, 3, 5, 7, 9, etc. The operator for this comparison is this:

(f1 ^ f2) & 1

The first part is responsible for providing a bit by bit comparison of the binary number.

(f1 ^ f2)

The remaining part is what strips away the result to leave only the last bit. If the final result is 1, we know that the last digits differ, and thus one is odd, the other is even, giving us boolean true, and the opposite 0 giving us false.

/*************************************************************
*                                                           *
* Title: Opposites Attract                                  *
* Author: krisyotam                                         *
* Date: 2025-08-23                                          *
* Writeup: notes.krisyotam.com/opposites-attract-c-codewars *
*                                                           *
*************************************************************/

bool lovefunc(int f1, int f2) { // take in variables for the amounts of flowers
  return (f1 ^ f2) & 1;
}

Link To Solution on CodeWars

Solution #2 (return boolean expression directly)

In this solution, we take advantage of the integer properties discussed earlier and deal directly with the sum of numbers, and return true or false based on whether it is odd or even.

bool lovefunc(int f1, int f2) {
    if ((f1 + f2) % 2 == 1){
        return true;
    } else {
        return false;
    }

}

Solution #3

Bibliography

@misc{codewars-opposites-attract}
author Codewars
title Opposites Attract
note C kata.
Last updated