切换数字的第一位和最后一位

给定一个数字n,任务是只切换数字的第一位和最后一位 例如:

null
Input : 10Output : 3Binary representation of 10 is1010. After toggling first andlast bits, we get 0011.Input : 15Output : 6

先决条件: 查找给定数字的MSB . 1) 生成一个包含设置的第一位和最后一位的数字。我们需要将所有中间位更改为0,并将角位保留为1。 2) 答案是生成的数字和原始数字的异或。

C++

// CPP program to toggle first and last
// bits of a number
#include <iostream>
using namespace std;
// Returns a number which has same bit
// count as n and has only first and last
// bits as set.
int takeLandFsetbits( int n)
{
// set all the bit of the number
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
// Adding one to n now unsets
// all bits and moves MSB to
// one place. Now we shift
// the number by 1 and add 1.
return ((n + 1) >> 1) + 1;
}
int toggleFandLbits( int n)
{
// if number is 1
if (n == 1)
return 0;
// take XOR with first and
// last set bit number
return n ^ takeLandFsetbits(n);
}
// Driver code
int main()
{
int n = 10;
cout << toggleFandLbits(n);
return 0;
}


JAVA

// Java program to toggle first and last
// bits of a number
import java.io.*;
class GFG {
// Returns a number which has same bit
// count as n and has only first and last
// bits as set.
static int takeLandFsetbits( int n)
{
// set all the bit of the number
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
// Adding one to n now unsets
// all bits and moves MSB to
// one place. Now we shift
// the number by 1 and add 1.
return ((n + 1 ) >> 1 ) + 1 ;
}
static int toggleFandLbits( int n)
{
// if number is 1
if (n == 1 )
return 0 ;
// take XOR with first and
// last set bit number
return n ^ takeLandFsetbits(n);
}
// Driver code
public static void main(String args[])
{
int n = 10 ;
System.out.println(toggleFandLbits(n));
}
}
/*This code is contributed by Nikita Tiwari.*/


Python3

# Python 3 program to toggle first
# and last bits of a number.
# Returns a number which has same bit
# count as n and has only first and last
# bits as set.
def takeLandFsetbits(n) :
# set all the bit of the number
n = n | n >> 1
n = n | n >> 2
n = n | n >> 4
n = n | n >> 8
n = n | n >> 16
# Adding one to n now unsets
# all bits and moves MSB to
# one place. Now we shift
# the number by 1 and add 1.
return ((n + 1 ) >> 1 ) + 1
def toggleFandLbits(n) :
# if number is 1
if (n = = 1 ) :
return 0
# take XOR with first and
# last set bit number
return n ^ takeLandFsetbits(n)
# Driver code
n = 10
print (toggleFandLbits(n))
# This code is contributed by Nikita Tiwari.


C#

// C# program to toggle first and last
// bits of a number
using System;
class GFG {
// Returns a number which has same bit
// count as n and has only first and last
// bits as set.
static int takeLandFsetbits( int n)
{
// set all the bit of the number
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
// Adding one to n now unsets
// all bits and moves MSB to
// one place. Now we shift
// the number by 1 and add 1.
return ((n + 1) >> 1) + 1;
}
static int toggleFandLbits( int n)
{
// if number is 1
if (n == 1)
return 0;
// take XOR with first and
// last set bit number
return n ^ takeLandFsetbits(n);
}
// Driver code
public static void Main()
{
int n = 10;
Console.WriteLine(toggleFandLbits(n));
}
}
// This code is contributed by Anant Agarwal.


PHP

<?php
// PHP program to toggle first and last
// bits of a number
// Returns a number which has same bit
// count as n and has only first and last
// bits as set.
function takeLandFsetbits( $n )
{
// set all the bit of the number
$n |= $n >> 1;
$n |= $n >> 2;
$n |= $n >> 4;
$n |= $n >> 8;
$n |= $n >> 16;
// Adding one to n now unsets
// all bits and moves MSB to
// one place. Now we shift
// the number by 1 and add 1.
return (( $n + 1) >> 1) + 1;
}
function toggleFandLbits(int $n )
{
// if number is 1
if ( $n == 1)
return 0;
// take XOR with first and
// last set bit number
return $n ^ takeLandFsetbits( $n );
}
// Driver code
$n = 10;
echo toggleFandLbits( $n );
// This code is contributed by mits
?>


Javascript

<script>
// JavaScript program to toggle first and last
// bits of a number
// Returns a number which has same bit
// count as n and has only first and last
// bits as set.
function takeLandFsetbits(n)
{
// set all the bit of the number
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
// Adding one to n now unsets
// all bits and moves MSB to
// one place. Now we shift
// the number by 1 and add 1.
return ((n + 1) >> 1) + 1;
}
function toggleFandLbits(n)
{
// if number is 1
if (n == 1)
return 0;
// take XOR with first and
// last set bit number
return n ^ takeLandFsetbits(n);
}
// Driver code
let n = 10;
document.write(toggleFandLbits(n));
</script>


输出:

3

时间复杂度为O(1)。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享