Haskell Program to get total Bits Required for the Given Number using Library Function


Haskell has internal function like finiteBitSize, ceiling, logBase, length and showIntAtBase that can be used to get total bits required from the given number. In the first example, we are going to use (bits = finiteBitSize (fromIntegral x :: Int)) function and in the second example, we are going to use (bitsRequired n = ceiling (logBase 2 (fromIntegral (abs n) + 1))) function. In the third example, we are going to use (bitsRequired n = length $ showIntAtBase 2 intToDigit (abs n) "") function.

Algorithm

  • Step 1 − Import the internal libraries

  • Step 2 − The bitsRequired function is defined using ceiling and logBase function

  • Step 3 − The 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, ‘x’ is defined to hold the number whose total bits are required to be computed.

  • Step 5 − The bitsRequired function is called and the number is passed as argument to it.

  • Step 6 − The resultant total bits required are printed to the console once the function is called.

Example 1

In this example, the total bits required for the given number is computed using finiteBitSize function.

import Data.Bits (FiniteBits, finiteBitSize)

main :: IO ()
main = do
    let x = 42 :: Int
        bits = finiteBitSize (fromIntegral x :: Int)
    putStrLn $ "The number " ++ show x ++ " requires " ++ show bits ++ " bits to represent."

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
The number 42 requires 64 bits to represent.

Example 2

In this example, the total bits required for the given number is computed using ceiling and logBase function.

import Numeric.Natural

bitsRequired :: Integer -> Int
bitsRequired n = ceiling (logBase 2 (fromIntegral (abs n) + 1))

main :: IO ()
main = do
  let x = 42

      bits = bitsRequired x
  putStrLn $ "The number " ++ show x ++ " requires " ++ show bits ++ " bits to represent."

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
The number 42 requires 6 bits to represent.

Example 3

In this example, the total bits required for the given number is computed using length and showIntAtBase function.

import Numeric (showIntAtBase)
import Data.Char (intToDigit)

bitsRequired :: Integer -> Int
bitsRequired n = length $ showIntAtBase 2 intToDigit (abs n) ""

main :: IO ()
main = do
  let x = 42
      bits = bitsRequired x
  putStrLn $ "The number " ++ show x ++ " requires " ++ show bits ++ " bits to represent."

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
The number 42 requires 6 bits to represent.

Conclusion

The total number of bits required by a number depends on the number itself and the number of bits used to represent it. For example, if we're using 32-bit integers, then the maximum value that can be represented is 2^31 - 1 = 2147483647, which requires 31 bits to represent. If we're using 64-bit integers, then the maximum value that can be represented is 2^63 - 1 = 9223372036854775807, which requires 63 bits to represent. We can use certain functions to compute the same in Haskell.

Updated on: 28-Mar-2023

41 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements