In: Computer Science
Write a function intLog of type Integer -> Integer that returns the exponent of the largest power of 2 less than its integer argument.
This needs to be a haskell function
#include <bits/stdc++.h>
using
namespace
std;
// Function to return the lowest power
// of 2 close to given positive number
int
powOfPositive(
int
n)
{
// Floor function is
used to determine
// the value close to
the number
int
pos
=
floor
(log2(n));
return
pow
(2, pos);
}
// Function to return the lowest power
// of 2 close to given negative number
int
powOfNegative(
int
n)
{
// Ceil function is
used for negative numbers
// as -1 > -4. It
would be opposite
// to positive
numbers where 1 < 4
int
pos
=
ceil
(log2(n));
return
(-1 *
pow
(2, pos));
}
// Function to find the highest power of 2
void
highestPowerOf2(
int
n)
{
// To check if the
given number
// is positive or
negative
if
(n
> 0) {
cout
<< powOfPositive(n);
}
else
{
//
If the number is negative,
//
then the ceil of the positive number
//
is calculated and
//
negative sign is added
n
= -n;
cout
<< powOfNegative(n);
}
}
// Driver code
int
main()
{
int
n;
cin>>n;
highestPowerOf2(n);
return
0;
}