Haskell Program to find the 1's complement of the given number


This tutorial will help us to find the 1's complement of the given number. The 1's complement of a binary number is found by inverting each bit of the number. A 1 becomes a 0, and a 0 becomes a 1. This is also known as the bitwise NOT operation. The 1's complement of a number can be useful in certain types of error-detecting and error-correcting Example:s, as well as in certain types of digital logic circuits. The most common use of 1's complement is in signed number representation where it's used to represent negative numbers.

For example, if the binary representation of a number is "1010", its 1's complement would be "0101".

Algorithm

  • Step 1 − The Data.Bits module is imported to use the complement function.

  • Step 2 − Define onesComplement function.

  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 4 − The variable named, “num” is being initialized. It will contain the number whose 1’s complement is to be computed.

  • Step 5 − The final resultant value is displayed by using ‘putStrLn’ statement and show function.

Using Complement Function

In this example, we will pass the number to the onesComplement function which then finds the 1's complement of the number using the complement function. Then it prints the 1's complement of the original number.

Example 1

import Data.Bits
onesComplement :: Int -> Int
onesComplement n = complement n
main :: IO ()
main = do
   let num = 10
   putStrLn $ "The 1's complement of " ++ show num ++ " is " ++ show (onesComplement num)

Output

The 1's complement of 10 is -11

Using bitSize Function

In this example, the onesComplement function uses the bitSize of the input number to calculate the maximum value that can be represented by that number of bits. Then it subtracts the input number from this maximum value to find the 1's complement.

Example 2

import Data.Bits
onesComplement :: Int -> Int
onesComplement n = (2^(bitSize n) - 1) - n

main :: IO ()
main = do
   let num = 10
   putStrLn $ "The 1's complement of " ++ show num ++ " is " ++ show (onesComplement num)

Output

The 1's complement of 10 is -11

Using xor Function

In this example, we will import the specific xor function and shiftL operator from the Data.Bits module to find 1’s complement of the given number.

Example 3

import Data.Bits (finiteBitSize, xor, shiftL)
onesComplement :: Int -> Int
onesComplement n = n `xor` (1 `shiftL` finiteBitSize n - 1)
main :: IO ()
main = do
   let num = 10
   putStrLn $ "The 1's complement of " ++ show num ++ " is " ++ show (onesComplement num)

Output

The 1's complement of 10 is -11

Conclusion

In Haskell, there are various ways to find the 1's complement of the given number. This can be achieved by using complement function, by using bitSize function or by using xor function. The 1's complement of a binary number is found by inverting each bit of the number. A 1 becomes a 0, and a 0 becomes a 1. This is also known as the bitwise NOT operation. The most common use of 1's complement is in signed number representation where it's used to represent negative numbers.

Updated on: 23-Jan-2023

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements