Member-only story

How To Get The Opposite Binary

Gokul
Oct 31, 2021

--

This post is mainly for those who are looking to get the opposite binary numbers programmatically. Here I will be explaining with Ruby, but this could work for all programming languages.

Let see the problem statement!!! If the binary input/value(‘0’ or ‘1’) passed to the method or function should return the opposite binary value.

Example:

  • If the value “1” is passed to the method/function should be getting the response as “0”.
  • If the value “0” is passed to the method/function should be getting the response as “1”.

Solution

# Ruby!def get_opposite_binary(value)
result = 1 - value
puts "Input: #{value} ---> Output: #{result}"
end
# Output:get_opposite_binary(0)
# Input: 0 ---> Output: 1
get_opposite_binary(1)
# Input: 1 ---> Output: 0

Thank you for reading my post. If you like it show your support with claps 👏.

Happy coding!!!

--

--

Gokul
Gokul

No responses yet