切换数字的所有偶数位

给定一个数字,任务是切换一个数字的所有偶数位 例如:

null
Input : 10Output : 0binary representation 1 0 1 0after toggle          0 0 0 0 Input : 20Output : 30binary representation 1 0 1 0 0after toggle          1 1 1 1 0

1.首先生成一个包含偶数位的数字。 2.使用原始数字进行异或运算。请注意,1^1=0和1^0=1。 让我们用下面的代码来理解这种方法。

C++

// CPP code to Toggle all even
// bit of a number
#include <iostream>
using namespace std;
// Returns a number which has all even
// bits of n toggled.
int evenbittogglenumber( int n)
{
// Generate number form of 101010
// ..till of same order as n
int res = 0, count = 0;
for ( int temp = n; temp > 0; temp >>= 1) {
// if bit is even then generate
// number and or with res
if (count % 2 == 1)
res |= (1 << count);
count++;
}
// return toggled number
return n ^ res;
}
// Driver code
int main()
{
int n = 11;
cout << evenbittogglenumber(n);
return 0;
}


JAVA

// Java code to Toggle all
// even bit of a number
import java.io.*;
class GFG {
// Returns a number which has
// all even bits of n toggled.
static int evenbittogglenumber( int n)
{
// Generate number form of 101010
// ..till of same order as n
int res = 0 , count = 0 ;
for ( int temp = n; temp > 0 ;
temp >>= 1 )
{
// if bit is even then generate
// number and or with res
if (count % 2 == 1 )
res |= ( 1 << count);
count++;
}
// return toggled number
return n ^ res;
}
// Driver code
public static void main(String args[])
{
int n = 11 ;
System.out.println(evenbittogglenumber(n));
}
}
// This code is contributed by Nikita Tiwari.


Python3

# Python code to Toggle all
# even bit of a number
# Returns a number which has all even
# bits of n toggled.
def evenbittogglenumber(n) :
# Generate number form of 101010
# ..till of same order as n
res = 0
count = 0
temp = n
while (temp > 0 ) :
# if bit is even then generate
# number and or with res
if (count % 2 = = 1 ) :
res = res | ( 1 << count)
count = count + 1
temp >> = 1
# return toggled number
return n ^ res
# Driver code
n = 11
print (evenbittogglenumber(n))
#This code is contributed by Nikita Tiwari.


C#

// C# code to Toggle all
// even bit of a number
using System;
class GFG {
// Returns a number which has
// all even bits of n toggled.
static int evenbittogglenumber( int n)
{
// Generate number form of 101010
// ..till of same order as n
int res = 0, count = 0;
for ( int temp = n; temp > 0;
temp >>= 1)
{
// if bit is even then generate
// number and or with res
if (count % 2 == 1)
res |= (1 << count);
count++;
}
// return toggled number
return n ^ res;
}
// Driver code
public static void Main()
{
int n = 11;
Console.WriteLine(evenbittogglenumber(n));
}
}
// This code is contributed by Anant Agarwal.


PHP

<?php
// php code to Toggle all
// even bit of a number
// Returns a number which has
// all even bits of n toggled.
function evenbittogglenumber( $n )
{
// Generate number form of 101010
// ..till of same order as n
$res = 0;
$count = 0;
for ( $temp = $n ; $temp > 0; $temp >>= 1)
{
// if bit is even then generate
// number and or with res
if ( $count % 2 == 1)
$res |= (1 << $count );
$count ++;
}
// return toggled number
return $n ^ $res ;
}
// Driver code
$n = 11;
echo evenbittogglenumber( $n );
// This code is contributed by mits
?>


Javascript

<script>
// JavaScript program to Toggle all
// even bit of a number
// Returns a number which has
// all even bits of n toggled.
function evenbittogglenumber(n)
{
// Generate number form of 101010
// ..till of same order as n
let res = 0, count = 0;
for (let temp = n; temp > 0;
temp >>= 1)
{
// if bit is even then generate
// number and or with res
if (count % 2 == 1)
res |= (1 << count);
count++;
}
// return toggled number
return n ^ res;
}
// Driver code
let n = 11;
document.write(evenbittogglenumber(n));
</script>


输出:

1

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