hdu 1196(位运算)
摘要:/* Name: 位运算应用 Copyright: Author: Try_86 Date: 12/04/12 22:24 Description: */#include <cstdio>#include <iostream>using namespace std;int main() { int a; while (scanf("%d", &a), a) printf ("%d\n", a&(a^(a-1))); return 0;}
阅读全文
posted @
2012-04-12 22:27
Try86
阅读(154)
推荐(0)
二进制逆序(分治思想)
摘要:实现自Matrix67!//二进制逆序//分治思想//程序读入一个32位整数并输出它的二进制倒序后所表示的数#include <stdio.h>int main() { int n; while (scanf("%d", &n) != EOF) { n = (n & 0x55555555) << 1 | (n & 0xAAAAAAAA) >> 1; n = (n & 0x33333333) << 2 | (n & 0xCCCCCCCC) >> 2; n = (n &
阅读全文
posted @
2012-04-05 12:53
Try86
阅读(396)
推荐(0)
位运算的应用续
摘要://用异或运算交换两个整数#include <stdio.h>int main() { int a, b; while (scanf("%d%d", &a, &b) != EOF) { a = a ^ b; b = a ^ b; a = a ^ b; printf ("%d %d\n", a, b); } return 0;}//用位运算来取绝对值#include <stdio.h>int main() { int n; while (scanf("%d", &n) != EOF) {
阅读全文
posted @
2012-04-05 12:25
Try86
阅读(160)
推荐(0)
高低位交换
摘要:实现自Matrix67!//定义:32位的整数的前16位为高位,后16位为低位//高低位交换#include <stdio.h>int main() { int n; while (scanf("%d", &n) != EOF) { int s, s1, s2; s1 = ((1 << 16) - 1) & n; s2 = s1 ^ n; s = (s1 << 16) | (s2 >> 16); printf ("%d\n", s); } return 0;}//定义:32位的整数的前16位
阅读全文
posted @
2012-04-05 12:18
Try86
阅读(444)
推荐(0)
计算二进制中的1的个数
摘要:实现自Matrix67!//计算二进制中的1的个数//分治思想#include <stdio.h>int main() { int n; while (scanf("%d", &n) != EOF) { n = (n & 0x55555555) + ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); n = (n & 0x0F0F0F0F) + ((n >> 4) & 0x0F0
阅读全文
posted @
2012-04-05 12:13
Try86
阅读(204)
推荐(0)
二进制中的1有奇数个还是偶数个
摘要:实现自Matrix67!//二进制中的1有奇数个还是偶数个//实现一#include <stdio.h>int main() { int n; while (scanf("%d", &n) != EOF) { int c = 0; while (n) { c += n & 1; n >>= 1; } if (c & 1) printf ("奇数个\n"); else printf ("偶数个\n"); } return 0;}//实现二//分治思想#inc...
阅读全文
posted @
2012-04-05 12:12
Try86
阅读(565)
推荐(0)
二分查找32位整数的前导0个数
摘要:参考自Matrix67!//二分查找32位整数的前导0个数//二分查找思想#include <stdio.h>int main() { unsigned int n; while (scanf("%u", &n) != EOF) { if (n == 0) printf ("32\n"); else { int s = 1; if ((n >> 16) == 0) {s += 16; n <<= 16;} if ((n >> 24) == 0) {s += 8; n <<= 8;} ..
阅读全文
posted @
2012-04-05 12:10
Try86
阅读(645)
推荐(0)
位运算的简单应用(C实现)
摘要:详细理论请参考Matrix67!//去掉最后一位,相当于除于2#include <stdio.h>int main() { int n; while (scanf("%d", &n) != EOF) { printf ("%d\n", n>>1); } return 0;}//在最后加一个0,相当于乘于2#include <stdio.h>int main() { int n; while (scanf("%d", &n) != EOF) { printf ("%d\n&q
阅读全文
posted @
2012-04-05 07:39
Try86
阅读(208)
推荐(0)