First and Last Three Bits
Last Updated :
09 Dec, 2023
Given an integer N. The task is to print the decimal equivalent of the first three bits and the last three bits in the binary representation of N.
Examples:
Input: 86
Output: 5 6
The binary representation of 86 is 1010110.
The decimal equivalent of the first three bits (101) is 5.
The decimal equivalent of the last three bits (110) is 6.
Hence the output is 5 6.
Input: 7
Output: 7 7
Simple Approach:
- Convert N into binary and store the bits in an array.
- Convert the first three values from the array into decimal equivalent and print it.
- Similarly, convert the last three values from the array into decimal equivalent and print it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void binToDecimal3( int n)
{
int a[64] = { 0 };
int x = 0, i;
for (i = 0; n > 0; i++) {
a[i] = n % 2;
n /= 2;
}
x = (i < 3) ? 3 : i;
int d = 0, p = 0;
for ( int i = x - 3; i < x; i++)
d += a[i] * pow (2, p++);
cout << d << " " ;
d = 0;
p = 0;
for ( int i = 0; i < 3; i++)
d += a[i] * pow (2, p++);
cout << d;
}
int main()
{
int n = 86;
binToDecimal3(n);
return 0;
}
|
Java
import java.math.*;
public class GFG {
static void binToDecimal3( int n)
{
int a[] = new int [ 64 ] ;
int x = 0 , i;
for (i = 0 ; n > 0 ; i++) {
a[i] = n % 2 ;
n /= 2 ;
}
x = (i < 3 ) ? 3 : i;
int d = 0 , p = 0 ;
for ( int j = x - 3 ; j < x; j++)
d += a[j] * Math.pow( 2 , p++);
System.out.print( d + " " );
d = 0 ;
p = 0 ;
for ( int k = 0 ; k < 3 ; k++)
d += a[k] * Math.pow( 2 , p++);
System.out.print(d);
}
public static void main(String[] args) {
int n = 86 ;
binToDecimal3(n);
}
}
|
Python3
from math import pow
def binToDecimal3(n):
a = [ 0 for i in range ( 64 )]
x = 0
i = 0
while (n > 0 ):
a[i] = n % 2
n = int (n / 2 )
i + = 1
if (i < 3 ):
x = 3
else :
x = i
d = 0
p = 0
for i in range (x - 3 , x, 1 ):
d + = a[i] * pow ( 2 , p)
p + = 1
print ( int (d), end = " " )
d = 0
p = 0
for i in range ( 0 , 3 , 1 ):
d + = a[i] * pow ( 2 , p)
p + = 1
print ( int (d),end = " " )
if __name__ = = '__main__' :
n = 86
binToDecimal3(n)
|
C#
using System;
class GFG
{
static void binToDecimal3( int n)
{
int [] a= new int [64] ;
int x = 0, i;
for (i = 0; n > 0; i++)
{
a[i] = n % 2;
n /= 2;
}
x = (i < 3) ? 3 : i;
int d = 0, p = 0;
for ( int j = x - 3; j < x; j++)
d += a[j] *( int )Math.Pow(2, p++);
int d1 = d;
d = 0;
p = 0;
for ( int k = 0; k < 3; k++)
d += a[k] * ( int )Math.Pow(2, p++);
Console.WriteLine(d1 + " " + d);
}
static void Main()
{
int n = 86;
binToDecimal3(n);
}
}
|
Javascript
function binToDecimal3(n)
{
var a = Array(64).fill(0);
var x = 0, i;
for (i = 0; n > 0; i++) {
a[i] = n % 2;
n = parseInt(n/2);
}
x = (i < 3) ? 3 : i;
var d = 0, p = 0;
for ( var i = x - 3; i < x; i++)
d += a[i] * parseInt(Math.pow(2, p++));
document.write(d + " " );
d = 0;
p = 0;
for ( var i = 0; i < 3; i++)
d += a[i] * parseInt(Math.pow(2, p++));
document.write(d);
}
var n = 86;
binToDecimal3(n);
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Efficient Approach:
We can use bitwise operators to find the required numbers.
C++
#include <bits/stdc++.h>
using namespace std;
void binToDecimal3( int n)
{
int last_3 = ((n & 4) + (n & 2) + (n & 1));
n = n >> 3;
while (n > 7)
n = n >> 1;
int first_3 = ((n & 4) + (n & 2) + (n & 1));
cout << first_3 << " " << last_3;
}
int main()
{
int n = 86;
binToDecimal3(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void binToDecimal3( int n)
{
int last_3 = ((n & 4 ) +
(n & 2 ) + (n & 1 ));
n = n >> 3 ;
while (n > 7 )
n = n >> 1 ;
int first_3 = ((n & 4 ) +
(n & 2 ) + (n & 1 ));
System.out.println(first_3 + " " + last_3);
}
public static void main(String args[])
{
int n = 86 ;
binToDecimal3(n);
}
}
|
Python3
def binToDecimal3(n) :
last_3 = ((n & 4 ) + (n & 2 ) + (n & 1 ));
n = n >> 3
while (n > 7 ) :
n = n >> 1
first_3 = ((n & 4 ) + (n & 2 ) + (n & 1 ))
print (first_3,last_3)
if __name__ = = "__main__" :
n = 86
binToDecimal3(n)
|
C#
using System;
class GFG
{
static void binToDecimal3( int n)
{
int last_3 = ((n & 4) +
(n & 2) + (n & 1));
n = n >> 3;
while (n > 7)
n = n >> 1;
int first_3 = ((n & 4) +
(n & 2) + (n & 1));
Console.WriteLine(first_3 + " " + last_3);
}
static public void Main ()
{
int n = 86;
binToDecimal3(n);
}
}
|
Javascript
<script>
function binToDecimal3(n)
{
var last_3 = ((n & 4) + (n & 2) + (n & 1));
n = n >> 3;
while (n > 7)
n = n >> 1;
var first_3 = ((n & 4) + (n & 2) + (n & 1));
document.write(first_3 + " " + last_3);
}
var n = 86;
binToDecimal3(n);
</script>
|
PHP
<?php
function binToDecimal3( $n )
{
$last_3 = (( $n & 4) + ( $n & 2) + ( $n & 1));
$n = $n >> 3;
while ( $n > 7)
$n = $n >> 1;
$first_3 = (( $n & 4) + ( $n & 2) + ( $n & 1));
echo ( $first_3 );
echo ( " " );
echo ( $last_3 );
}
$n = 86;
binToDecimal3( $n );
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach#3: Using slicing
Read an integer input from the user. Convert the integer to its binary representation using the bin() function. Remove the ‘0b’ prefix from the binary representation using string slicing. Get the first three bits of the binary representation using string slicing. Get the last three bits of the binary representation using string slicing. Convert the binary strings of the first and last three bits to decimal integers using the int() function with the second argument set to 2 (which means the input string is interpreted as a binary string). Print the decimal values of the first and last three bits.
Algorithm
1. Start the program.
2. Read an integer input from the user and store it in a variable num.
3. Convert the integer to its binary representation using the bin() function and store it in a variable bin_num.
4. Remove the ‘0b’ prefix from the binary representation using string slicing and store the result in bin_num.
5. Get the first three bits of the binary representation using string slicing and store the result in a variable first_three_bits.
6. Get the last three bits of the binary representation using string slicing and store the result in a variable last_three_bits.
7. Convert the binary strings of the first and last three bits to decimal integers using the int() function with the second argument set to 2 and store the results in variables first_decimal and last_decimal.
8. Print the decimal values of the first and last three bits.
9. End the program.
C++
#include <iostream>
#include <bitset>
int main() {
int num = 86;
std::bitset<8> bin_num(num);
std::string bin_str = bin_num.to_string();
std::string first_three_bits = bin_str.substr(3, 3);
std::string last_three_bits = bin_str.substr(bin_str.size() - 3);
int first_three_decimal = std::stoi(first_three_bits, nullptr, 2);
int last_three_decimal = std::stoi(last_three_bits, nullptr, 2);
std::cout << first_three_decimal << " " << last_three_decimal << std::endl;
return 0;
}
|
Java
public class BinaryConversion {
public static void main(String[] args) {
int num = 86 ;
String binNum = Integer.toBinaryString(num);
String binNumWithoutPrefix = binNum.substring( 2 );
String firstThreeBits = binNumWithoutPrefix.substring( 0 , 3 );
String lastThreeBits = binNumWithoutPrefix.substring(binNumWithoutPrefix.length() - 3 );
int firstThreeBitsDecimal = Integer.parseInt(firstThreeBits, 2 );
int lastThreeBitsDecimal = Integer.parseInt(lastThreeBits, 2 );
System.out.println(firstThreeBitsDecimal + " " + lastThreeBitsDecimal);
}
}
|
Python3
num = 86
bin_num = bin (num)[ 2 :]
first_three_bits = bin_num[: 3 ]
last_three_bits = bin_num[ - 3 :]
print ( int (first_three_bits, 2 ), int (last_three_bits, 2 ))
|
C#
using System;
class BinaryConversion {
static void Main()
{
int num = 86;
string binNum = Convert.ToString(
num,
2);
string binNumWithoutPrefix
= binNum.Substring(2);
string firstThreeBits
= binNumWithoutPrefix.Substring(0, 3);
string lastThreeBits
= binNumWithoutPrefix.Substring(
binNumWithoutPrefix.Length - 3);
int firstThreeBitsDecimal
= Convert.ToInt32(firstThreeBits, 2);
int lastThreeBitsDecimal
= Convert.ToInt32(lastThreeBits, 2);
Console.WriteLine(firstThreeBitsDecimal + " "
+ lastThreeBitsDecimal);
}
}
|
Javascript
function decToBinary(num) {
return (num >>> 0).toString(2);
}
function main() {
const num = 86;
const binNum = decToBinary(num);
const binNumWithoutPrefix = binNum.substring(2);
const firstThreeBits = binNumWithoutPrefix.substring(0, 3);
const lastThreeBits = binNumWithoutPrefix.substring(binNumWithoutPrefix.length - 3);
const firstThreeBitsDecimal = parseInt(firstThreeBits, 2);
const lastThreeBitsDecimal = parseInt(lastThreeBits, 2);
console.log(firstThreeBitsDecimal + " " + lastThreeBitsDecimal);
}
main();
|
Time complexity: O(1) The time complexity of this program is constant because it performs a fixed number of operations that do not depend on the input size.
Space complexity: O(log n) The space complexity of this program depends on the size of the input integer. The bin() function returns a string representation of the binary number, which requires O(log n) space to store, where n is the input integer. The other variables created in the program (first_three_bits, last_three_bits, first_decimal, and last_decimal) all require constant space. Therefore, the overall space complexity is O(log n).
Share your thoughts in the comments
Please Login to comment...