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)$ ...
随机推荐
- IOS之UITableView——怎样刷新父页面的Cell
问题:评论数同步 在社交相关的项目中常常有这种主页面,主列表的Cell中有赞数.评论数,详情页顶部也是相同的一个Cell,下部有评论列表,评论添加或降低.详情页的评论数随之改变,返回主列表,主列表的相 ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 拓扑排序
C. Mail Stamps One day Bob got a letter in an envelope. Bob knows that when Berland's post offic ...
- ES 内存使用和GC指标——主节点每30秒会去检查其他节点的状态,如果任何节点的垃圾回收时间超过30秒(Garbage collection duration),则会导致主节点任务该节点脱离集群。
摘录自:http://blog.csdn.net/yangwenbo214/article/details/74000458 内存使用和GC指标 在运行Elasticsearch时,内存是您要密切监控 ...
- C# Keywords - is
记录一下在日常开发过程中遇到的一些C# 基础编程的知识!希望以后能用的着.知识是在平常的开发过程中去学到的.只有用到了,你才能深入的理解它,并用好它. 本资料来源于:MSND下面是一些相关的code ...
- 基于QMP实现对qemu虚拟机进行交互
本文详解QMP,包含qmp.hmp.qemu-guest-agent的介绍.工作原理.配置方法.范例 小慢哥的原创文章,欢迎转载 目录 ▪ QMP介绍 ▪ QMP语法 ▪ 单独使用qemu,启用QMP ...
- $.ajax 和$.post的区别
https://blog.csdn.net/weixin_39709686/article/details/78680754
- POJ 3020 Hungary
一道建图题-- // by SiriusRen #include <cstdio> #include <cstring> using namespace std; #defin ...
- 结构化编程-Structured programming
结构话编程强调的是对流程的控制: 它为面向过程编程提供天然的支持. Structured programming is a programming paradigm aimed at improvin ...
- 双十一只是在搞噱头么?看看ABBYY FineReader就知道了
双十一就这样轰轰烈烈的度过了,想问恢复正常工作日的你还好么,还能好好工作么,十分钟查一次物流的有木有? 由于ABBYY FineReader 12 限量200套半价活动过于火爆,各位小主也是用实际行动 ...
- Servlet的生命周期和Jsp的生命周期
Servlet的生命周期: 1)构造方法(第1次访问) 2)init方法(第1次访问) 3)service方法 4)destroy方法 Jsp的生命周期 1)翻译: jsp->java文件 2) ...