实例二:取n的第k位

方法:a>> k & 1
某值a右移K位后与整数“1”进行与运算。即把需要第几位就右移几位。

例子:

        0000 1000 ------8
右移3位     0000 0001 ------1
与1进行与操作  0000 0001 ------1

结果:       0000 0001 ------1

代码:
int _tmain(int argc, _TCHAR* argv[])
{
  int nValue,k,nResult=0;
  cout << "请输入要进行处理的数字:";
  cin >> nValue;
  cout << endl << "请输入要进行取的第几位:";
  cin >> k;
  if (nValue != 1)
  {
    nResult = nValue >> k & 1;
    cout << "该位的值:" << nResult << endl;
  }
  else
  {  
    cout << "该位的值:1" <<endl;
  }
  system("pause");
  return 0;
};

取n的第k位的更多相关文章

  1. Algorithm --> n位数去掉k位后找最小数

    去掉K位求取最小数 一个n位的数,去掉其中的k位,怎样使留下来的(n-k)位数按原来的前后顺序组成的数最小 例如 8314925去掉4个数,留下125最小,注意有前后顺序要求,要是没有顺序当然是123 ...

  2. Leading and Trailing LightOJ - 1282 (取数的前三位和后三位)

    题意: 求n的k次方的前三位 和 后三位 ...刚开始用 Java的大数写的...果然超时... 好吧  这题用快速幂取模求后三位  然后用一个技巧求前三位 ...orz... 任何一个数n均可以表示 ...

  3. 将n的k位清0

    实例三:将n的k位清0 方法: result= n &~(1<<k) 使第k为变成0,再与运算,0和任何数进行与运算都是0. 解释:  0000 0001 ---- 1 左移k位 ...

  4. Leetcode 402.移掉k位数字

    移调k位数字 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零. 示例 1 : ...

  5. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  6. ACM1229_还是A+B(求A的第K位的数公式:A%((int)(pow(10,K)))

    #include<stdio.h> #include<math.h> int main() { int A,k,B,sum,c,d; while(scanf("%d% ...

  7. 把一个数组向右循环移动k位要求时间复杂度为O(n)

    今晚做了下某公司的网络笔试题,好久没刷题了,现在渣得要死,里面有道程序设计题是 把一个数组向右循环移动k位要求时间复杂度为O(n) 给的方法定义为 public void solution(int a ...

  8. [Swift]LeetCode402. 移掉K位数字 | Remove K Digits

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  9. A + B,末k位不相同

    题目描述 读入两个小于10000的正整数A和B,计算A+B.需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1. 输入描述: 测试输入包含若干测试用例,每个测试用例占一行,格式为& ...

随机推荐

  1. Flink - allowedLateness

    WindowOperator processElement final Collection<W> elementWindows = windowAssigner.assignWindow ...

  2. jpa持久化对象四种状态

    自己理解,不完全正确,大致如下: 例:某实体类   Person(int id,string name,int age);   id 为主键. 新建:new Person(),  并且未给 id 赋值 ...

  3. GDB常用命令系列

    本文由霸气的菠萝原创,转载请注明出处:http://www.cnblogs.com/xsln/p/gdb_instructions.html 本文为索引,请点击以下链接进行阅读: GDB调试原理——p ...

  4. 对线程发送signal

    学习对线程 发送 signal #include <stdio.h> #include <stdlib.h> #include <string.h> #includ ...

  5. Requirejs 使用

    代码地址 参考地址1 参考地址2 一.不依赖其他模块的module创建 创建math的module // math.js define(function (){ var add = function ...

  6. ubuntu上make menuconfig出错

    如果使用make menuconfig的方式配置内核,又碰巧系统没有安装ncurses库(ubuntu系统默认就没有安装此库),就会出现错误,错误信息大体上如下: *** Unable to find ...

  7. LeetCode-300.Longst Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  8. python json 模块

    什么是json? json是返回的是字符串格式,把python数据类型列表.字典转换成json字符串格式, 这种格式java php 其他语言都可以认识的字符串,可以跨语言交流. json,用于字符串 ...

  9. 多态使用时,父类多态时需要使用子类特有对象。需要判断 就使用instanceof

    instanceof:通常在向下转型前用于健壮性的判断,判断是符合哪一个子类对象 package Polymorphic; public class TestPolymorphic { public ...

  10. 如何使用向量代表文档doc或者句子sentence

    1.“句向量”简介word2vec提供了高质量的词向量,并在一些任务中表现良好. 关于word2vec的原理可以参考这几篇论文: https://arxiv.org/pdf/1310.4546.pdf ...