Binary operations #1
https://www.codewars.com/kata/binary-operations-number-1/train/csharp
Your work is to write a method that takes a value and an index, and returns the value with the bit at given index flipped.
The bits are numbered from the least significant bit (index 1).
Example:
FlipBit(15,4) == 7 // 15 in binary is 1111, after flipping 4th bit, it becomes 0111, i.e. 7
FlipBit(15,5) == 31 // 15 in binary is 1111, 5th bit is 0, after flipping, it becomes 11111, i.e., 31
using System;
public class Flip
{
public static int FlipBit(int value, int bitIndex)
{
string str = Convert.ToString(value, ).PadLeft(,'');
var array = str.ToCharArray();
int index = - bitIndex;
if (array[index] == '')
{
array[index] = '';
}
else
{
array[index] = '';
}
str = string.Join(string.Empty,array);
return Convert.ToInt32(str,);
}
}
对于给定的value,反转index位置上对应的bit。index从最右边开始为第一位。
using System;
public class Flip
{
public static int FlipBit(int value, int bitIndex)
{
int Mask = << (bitIndex - );
return value ^ Mask;
}
}
Binary operations #1的更多相关文章
- Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...
- D. GukiZ and Binary Operations(矩阵+二进制)
D. GukiZ and Binary Operations We all know that GukiZ often plays with arrays. Now he is thinking ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- uestc 1709 Binary Operations 位运算的灵活运用
Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674 Description B ...
- Codeforces 551 D. GukiZ and Binary Operations
\(>Codeforces \space 551 D. GukiZ and Binary Operations<\) 题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) ...
- 位运算 UEST 84 Binary Operations
题目传送门 题意:所有连续的子序列的三种位运算计算后的值的和的期望分别是多少 分析:因为所有连续子序列的组数有n * (n + 1) / 2种,所以要将他们分类降低复杂度,以ai为结尾的分成一组,至于 ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations
得到k二进制后,对每一位可取得的方法进行相乘即可,k的二进制形式每一位又分为2种0,1,0时,a数组必定要为一长为n的01串,且串中不出现连续的11,1时与前述情况是相反的. 且0时其方法总数为f(n ...
- GukiZ and Binary Operations CodeForces - 551D (组合计数)
大意: 给定$n,k,l,m$, 求有多少个长度为$n$, 元素全部严格小于$2^l$, 且满足 的序列. 刚开始想着暴力枚举当前or和上一个数二进制中$1$的分布, 但这样状态数是$O(64^3)$ ...
随机推荐
- 大型Web 站点 Asp.net Session过期你怎么办
在 WEB 系统中. 我们通常会用session来保存一些简单可是却非常重要的信息.比方Asp.net中常常会用Session来保存用户登录信息,比方UserID.为了解决 WEB场大家採用了把ses ...
- Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP
E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coinci ...
- 0x11 栈
这个不难吧,算是常识了..毕竟也是刷过USACO的人 对顶栈这东西前几天才遇到过,好像和在线求中位数那东西放一起了吧 单调栈倒是没什么...贴个代码算了.一开始有点蠢的每个位置算,后来发现出栈再算就行 ...
- 锋利Jquery 第一天
之前一直学习,现在终于有时间来整理一下文档了. 以下文章都是自己学习Jquery 的笔记, 希望能留下痕迹,也希望能帮助到您. 好了开始我的Jquery第一天. 我也是从Hello wrod!开始的 ...
- Spark SQL 编程API入门系列之SparkSQL数据源
不多说,直接上干货! SparkSQL数据源:从各种数据源创建DataFrame 因为 spark sql,dataframe,datasets 都是共用 spark sql 这个库的,三者共享同样的 ...
- IIS之虚拟目录学习
从刚实习开始就了解到虚拟目录这个词,但是一直没去研究过什么意思,而且也没实际用过.一晃两年过去了,今天正好趁休息,补补脑学习下. 通过百度了解到,虚拟目录创建的目的是为了应对磁盘容量爆满,部署的网站不 ...
- Hua Wei 机试题目三---2014
一.根据对应规则进行翻译输出 描述:已知有如下的对应规则: ,则输入任意个正整数,输出经过规则翻译以后对应的结果. 例如:输入:1234:输出bcde. 题目很简单,我觉得需要注意的问题就是对于大整数 ...
- JavaScript基础知识-正则表达式
正则表达式 作用:定义一个特定的验证字符串内容规则的表达式 注:正则表达式并不是JavaScript独有的:JavaScript支持正则表达式 var a = { }; // 定义一个空对象 ...
- deploy sql clr
1, create strong signed key file 2, create asymmetric key
- ajax返回值
前端: <html> <head> <meta name="viewport" content="width=device-width&qu ...