Given a number n, the task is to toggle only first and last bits of a number
Examples :
Input : 10
Output : 3
Binary representation of 10 is
1010. After toggling first and
last bits, we get 0011.
Input : 15
Output : 6
Prerequisite : Find MSB of given number.
1) Generate a number which contains first and last bit as set. We need to change all middle bits to 0 and keep corner bits as 1.
2) Answer is XOR of generated number and original number.
C++
#include <iostream>
using namespace std;
int takeLandFsetbits( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return ((n + 1) >> 1) + 1;
}
int toggleFandLbits( int n)
{
if (n == 1)
return 0;
return n ^ takeLandFsetbits(n);
}
int main()
{
int n = 10;
cout << toggleFandLbits(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int takeLandFsetbits( int n)
{
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
return ((n + 1 ) >> 1 ) + 1 ;
}
static int toggleFandLbits( int n)
{
if (n == 1 )
return 0 ;
return n ^ takeLandFsetbits(n);
}
public static void main(String args[])
{
int n = 10 ;
System.out.println(toggleFandLbits(n));
}
}
|
Python3
def takeLandFsetbits(n) :
n = n | n >> 1
n = n | n >> 2
n = n | n >> 4
n = n | n >> 8
n = n | n >> 16
return ((n + 1 ) >> 1 ) + 1
def toggleFandLbits(n) :
if (n = = 1 ) :
return 0
return n ^ takeLandFsetbits(n)
n = 10
print (toggleFandLbits(n))
|
C#
using System;
class GFG {
static int takeLandFsetbits( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return ((n + 1) >> 1) + 1;
}
static int toggleFandLbits( int n)
{
if (n == 1)
return 0;
return n ^ takeLandFsetbits(n);
}
public static void Main()
{
int n = 10;
Console.WriteLine(toggleFandLbits(n));
}
}
|
PHP
<?php
function takeLandFsetbits( $n )
{
$n |= $n >> 1;
$n |= $n >> 2;
$n |= $n >> 4;
$n |= $n >> 8;
$n |= $n >> 16;
return (( $n + 1) >> 1) + 1;
}
function toggleFandLbits(int $n )
{
if ( $n == 1)
return 0;
return $n ^ takeLandFsetbits( $n );
}
$n = 10;
echo toggleFandLbits( $n );
?>
|
Javascript
<script>
function takeLandFsetbits(n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return ((n + 1) >> 1) + 1;
}
function toggleFandLbits(n)
{
if (n == 1)
return 0;
return n ^ takeLandFsetbits(n);
}
let n = 10;
document.write(toggleFandLbits(n));
</script>
|
Time Complexity: O(1).
Auxiliary Space: O(1).
Another Approach:
We can do this in two steps by:
To generate an bit mask for n, where only the last and first bits are set, we need to calculate 2log2n + 20
Then, just take the XOR of the mask and n.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int toggleFandLbits( int n)
{
int mask = pow (2, ( int )log2(n)) + 1;
return mask ^ n;
}
int main()
{
int n = 10;
cout << toggleFandLbits(n);
return 0;
}
|
Java
class GFG
{
static int toggleFandLbits( int n)
{
int mask = ( int )Math.pow(
2 , ( int )(Math.log(n) / Math.log( 2 )))
+ 1 ;
return mask ^ n;
}
public static void main(String[] args)
{
int n = 10 ;
System.out.println(toggleFandLbits(n));
}
}
|
Python3
from math import log
def toggleFandLbits(n):
mask = 2 * * ( int (log(n, 2 ))) + 1
return mask ^ n
n = 10
print (toggleFandLbits(n))
|
C#
using System;
class GFG {
static int toggleFandLbits( int n)
{
int mask = ( int )Math.Pow(
2, ( int )(Math.Log(n) / Math.Log(2)))
+ 1;
return mask ^ n;
}
public static void Main( string [] args)
{
int n = 10;
Console.WriteLine(toggleFandLbits(n));
}
}
|
Javascript
function toggleFandLbits(n)
{
let mask = Math.pow(2, Math.floor(Math.log2(n))) + 1;
return mask ^ n;
}
let n = 10;
console.log(toggleFandLbits(n));
|
Time Complexity: O(log2(log2n)), as pow(log(n)) will be calculated to complexity will be log(log(n)).
Auxiliary Space: O(1).