Skip to content
Related Articles

Related Articles

First and Last Three Bits
  • Difficulty Level : Medium
  • Last Updated : 25 Mar, 2021

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:
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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the first
// and last 3 bits equivalent decimal number
void binToDecimal3(int n)
{
    // Converting n to binary
    int a[64] = { 0 };
    int x = 0, i;
    for (i = 0; n > 0; i++) {
        a[i] = n % 2;
        n /= 2;
    }
 
    // Length of the array has to be at least 3
    x = (i < 3) ? 3 : i;
 
    // Convert first three bits to decimal
    int d = 0, p = 0;
    for (int i = x - 3; i < x; i++)
        d += a[i] * pow(2, p++);
 
    // Print the decimal
    cout << d << " ";
 
    // Convert last three bits to decimal
    d = 0;
    p = 0;
    for (int i = 0; i < 3; i++)
        d += a[i] * pow(2, p++);
 
    // Print the decimal
    cout << d;
}
 
// Driver code
int main()
{
    int n = 86;
 
    binToDecimal3(n);
    return 0;
}

Java




//Java implementation of the approach
 
import java.math.*;
public class GFG {
 
    //Function to print the first
    //and last 3 bits equivalent decimal number
    static void binToDecimal3(int n)
    {
     // Converting n to binary
     int a[] = new int[64] ;
     int x = 0, i;
     for (i = 0; n > 0; i++) {
         a[i] = n % 2;
         n /= 2;
     }
 
     // Length of the array has to be at least 3
     x = (i < 3) ? 3 : i;
 
     // Convert first three bits to decimal
     int d = 0, p = 0;
     for (int j = x - 3; j < x; j++)
         d += a[j] * Math.pow(2, p++);
 
     // Print the decimal
     System.out.print( d + " ");
 
     // Convert last three bits to decimal
     d = 0;
     p = 0;
     for (int k = 0; k < 3; k++)
         d += a[k] * Math.pow(2, p++);
 
     // Print the decimal
     System.out.print(d);
    }
 
    //Driver code
    public static void main(String[] args) {
         
        int n = 86;
 
         binToDecimal3(n);
 
    }
 
}

Python3




# Python 3 implementation of the approach
from math import pow
 
# Function to print the first and last 3
# bits equivalent decimal number
def binToDecimal3(n):
     
    # Converting n to binary
    a = [0 for i in range(64)]
    x = 0
    i = 0
    while(n > 0):
        a[i] = n % 2
        n = int(n / 2)
        i += 1
 
    # Length of the array has to
    # be at least 3
    if (i < 3):
        x = 3
    else:
        x = i
 
    # Convert first three bits to decimal
    d = 0
    p = 0
    for i in range(x - 3, x, 1):
        d += a[i] * pow(2, p)
        p += 1
 
    # Print the decimal
    print(int(d), end =" ")
 
    # Convert last three bits to decimal
    d = 0
    p = 0
    for i in range(0, 3, 1):
        d += a[i] * pow(2, p)
        p += 1
 
    # Print the decimal
    print(int(d),end = " ")
 
# Driver code
if __name__ == '__main__':
    n = 86
 
    binToDecimal3(n)
     
# This code is contributed by
# Sanjit_Prasad

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print the first and last
// 3 bits equivalent decimal number
static void binToDecimal3(int n)
{
     
    // Converting n to binary
    int [] a= new int[64] ;
    int x = 0, i;
    for (i = 0; n > 0; i++)
    {
        a[i] = n % 2;
        n /= 2;
    }
 
    // Length of the array has to be
    // at least 3
    x = (i < 3) ? 3 : i;
     
    // Convert first three bits to decimal
    int d = 0, p = 0;
    for (int j = x - 3; j < x; j++)
        d += a[j] *(int)Math.Pow(2, p++);
     
    // Print the decimal
    int d1 = d;
     
    // Convert last three bits to decimal
    d = 0;
    p = 0;
    for (int k = 0; k < 3; k++)
        d += a[k] * (int)Math.Pow(2, p++);
     
    // Print the decimal
    Console.WriteLine(d1 + " " + d);
}
 
// Driver code
static void Main()
{
    int n = 86;
 
    binToDecimal3(n);
}
}
 
// This code is contributed by Mohit kumar 29

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to print the first
// and last 3 bits equivalent decimal number
function binToDecimal3(n)
{
    // Converting n to binary
    var a = Array(64).fill(0);
    var x = 0, i;
    for (i = 0; n > 0; i++) {
        a[i] = n % 2;
        n = parseInt(n/2);
    }
 
    // Length of the array has to be at least 3
    x = (i < 3) ? 3 : i;
 
    // Convert first three bits to decimal
    var d = 0, p = 0;
    for (var i = x - 3; i < x; i++)
        d += a[i] * parseInt(Math.pow(2, p++));
 
    // Print the decimal
    document.write(d + " ");
 
    // Convert last three bits to decimal
    d = 0;
    p = 0;
    for (var i = 0; i < 3; i++)
        d += a[i] * parseInt(Math.pow(2, p++));
 
    // Print the decimal
    document.write(d);
}
 
// Driver code
var n = 86;
binToDecimal3(n);
     
</script>
Output: 
5 6

 

Efficient Approach: 
We can use bitwise operators to find the required numbers. 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the first
// and last 3 bits equivalent decimal
// number
void binToDecimal3(int n)
{
    // Number formed from last three
    // bits
    int last_3 = ((n & 4) + (n & 2) + (n & 1));
 
    // Let us get first three bits in n
    n = n >> 3;
    while (n > 7)
        n = n >> 1;
 
    // Number formed from first three
    // bits
    int first_3 = ((n & 4) + (n & 2) + (n & 1));
 
    // Printing result
    cout << first_3 << " " << last_3;
}
 
// Driver code
int main()
{
    int n = 86;
    binToDecimal3(n);
    return 0;
}

Java




// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to print the first
// and last 3 bits equivalent
// decimal number
static void binToDecimal3(int n)
{
    // Number formed from last three
    // bits
    int last_3 = ((n & 4) +
                  (n & 2) + (n & 1));
 
    // Let us get first three bits in n
    n = n >> 3;
    while (n > 7)
        n = n >> 1;
 
    // Number formed from first
    // three bits
    int first_3 = ((n & 4) +
                   (n & 2) + (n & 1));
 
    // Printing result
    System.out.println(first_3 + " " + last_3);
}
 
// Driver code
public static void main(String args[])
{
    int n = 86;
    binToDecimal3(n);
}
}
 
// This code is contributed by
// Surendra_Gangwar

Python3




# Python3 implementation of the approach
 
# Function to print the first and
# last 3 bits equivalent decimal
# number
def binToDecimal3(n) :
     
    # Number formed from last three
    # bits
    last_3 = ((n & 4) + (n & 2) + (n & 1));
 
    # Let us get first three bits in n
    n = n >> 3
    while (n > 7) :
        n = n >> 1
 
    # Number formed from first three
    # bits
    first_3 = ((n & 4) + (n & 2) + (n & 1))
 
    # Printing result
    print(first_3,last_3)
 
# Driver code
if __name__ == "__main__" :
 
    n = 86
    binToDecimal3(n)
 
# This code is contributed by Ryuga

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to print the first
// and last 3 bits equivalent
// decimal number
static void binToDecimal3(int n)
{
    // Number formed from last three
    // bits
    int last_3 = ((n & 4) +
                (n & 2) + (n & 1));
 
    // Let us get first three bits in n
    n = n >> 3;
    while (n > 7)
        n = n >> 1;
 
    // Number formed from first
    // three bits
    int first_3 = ((n & 4) +
                (n & 2) + (n & 1));
 
    // Printing result
    Console.WriteLine(first_3 + " " + last_3);
}
 
// Driver code
static public void Main ()
{
    int n = 86;
    binToDecimal3(n);
}
}
 
// This code is contributed by akt_mit..

PHP




<?php
// PHP implementation of the approach
 
// Function to print the first and last
// 3 bits equivalent decimal number
function binToDecimal3($n)
{
    // Number formed from last three
    // bits
    $last_3 = (($n & 4) + ($n & 2) + ($n & 1));
 
    // Let us get first three bits in n
    $n = $n >> 3;
    while ($n > 7)
        $n = $n >> 1;
 
    // Number formed from first three
    // bits
    $first_3 = (($n & 4) + ($n & 2) + ($n & 1));
 
    // Printing result
    echo($first_3);
    echo(" ");
    echo($last_3);
}
 
// Driver code
$n = 86;
binToDecimal3($n);
 
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript




  <script>
 
    // Javascript implementation of the approach
 
    // Function to print the first
    // and last 3 bits equivalent decimal
    // number
    function binToDecimal3(n)
    {
     
      // Number formed from last three
      // bits
      var last_3 = ((n & 4) + (n & 2) + (n & 1));
 
      // Let us get first three bits in n
      n = n >> 3;
      while (n > 7)
        n = n >> 1;
 
      // Number formed from first three
      // bits
      var first_3 = ((n & 4) + (n & 2) + (n & 1));
 
      // Printing result
      document.write(first_3 + " " + last_3);
    }
 
    // Driver code
    var n = 86;
    binToDecimal3(n);
 
// This code is contributed by rrrtnx.
  </script>
Output: 
5 6

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :