- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Swift Program to find the 1's complement of the given number
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1's and 2's complement of 16-bit number
- Draw a Turing machine to find 1’s complement of a binary number
- 1’s and 2’s complement of a Binary Number?
- Haskell Program to find the reverse of a given number using recursion
- C Program to find two’s complement for a given number
- Haskell Program to get the Magnitude of the given number
- Haskell Program to calculate the square root of the given number
- Haskell Program to calculate the cube root of the given number
- Haskell Program to Calculate the Logarithm Gamma of the Given Number
- Haskell Program to find the arcsine of the given value
- Haskell Program to find the arccosine of the given value
- Haskell Program to find the arctangent of the given value
- Haskell Program To Find The Perfect Number
