#include "myBit.h"
void bitApp()
{
// Even or odd
//EvenOrOdd();
// Change two integer
//Exchange();
// Change sign 改变符号
//changeSign();
// absolute value 取绝对值
//absoluteValue();
// 整数的高位低位互换
//HighLowChange();
// 二进制逆序
//reverseBinary();
// Count 1 in binary
//countOneInBinary();
// 寻找缺失的数
FindLoseNum();
}
// 寻找成对出现数字中 缺失的数 例如 2,2,3,4,4,5,5,....缺少了3
// 要求成对的数字不重复
void FindLoseNum()
{
printf("======== 寻找缺失的数 ===========\n");
int a[] = {1, 23, 23, 4, 2, 3, 4, 3, 2};
int num = sizeof(a)/sizeof(int);
printf("原数组为: ");
int i = 0;
for(i = 0; i < num; i++)
{
printf("%d ", a[i]);
}
printf("\n");
int lost = 0;
for(i = 0; i < num; i++)
{
lost ^= a[i];
}
printf("缺失的数为: %d\n", lost);
}
void reverseBinary()
{
printf("==== 整数的二进制逆序 =====\n");
unsigned short a = 0xD2F3;
printf("逆序前:");
printBinary(a);
unsigned short b = a;
b = ((b & 0xAAAA) >> 1) | ( (b & 0x5555) << 1);
b = ((b & 0xCCCC) >> 2) | ( (b & 0x3333) << 2);
b = ((b & 0xF0F0) >> 4) | ( (b & 0x0F0F) << 4);
b = ((b & 0xFF00) >> 8) | ( (b & 0x00FF) << 8);
printf("逆序后:");
printBinary(b);
}
void countOneInBinary()
{
int a = 0x5;
printf("the number in %d is %d\n", a, count_binary_number(a));
printf("the 1 number in %d is %d\n", a, count_1_number(a));
}
int count_1_number(int x)
{
int cnt = 0;
while(x)
{
x = x & (x -1);
cnt ++;
}
return cnt;
}
int count_binary_number(int x)
{
int cnt = 0;
while(x)
{
x = x>>1;
cnt++;
}
return cnt;
}
void printBinary(unsigned short a)
{
int i = 0;
for(i = sizeof(a)*8 -1; i >= 0; i--)
{
if((a >> i) & 1)
{
putchar('1');
}else
{
putchar('0');
}
if(i % 8 == 0)
{
putchar(' ');
}
}
putchar('\n');
}
void HighLowChange()
{
printf("======== 高低位交换 ========\n");
unsigned short a = 34520;
printf("交换前: ");
printBinary(a);
unsigned short x = (a >> 8) | (a << 8);
printf("交换后: ");
printBinary(x);
}
void absoluteValue()
{
int a = -11, b = 324;
printf("a = %d, and b = %d \n", a, b);
a = _absoluteByBit(a);
b = _absoluteByBit(b);
printf("after absolute a = %d, and b = %d \n", a, b);
}
int _absoluteByBit(int a)
{
return (a >> 31) ? (~a + 1) : a;
}
void changeSign()
{
int a = -11, b = 324;
printf("a = %d, and b = %d \n", a, b);
a = _reverseSign(a);
b = _reverseSign(b);
printf("after reverse a = %d, and b = %d \n", a, b);
}
int _reverseSign(int a)
{
return (~a) + 1;
}
void Exchange()
{
int a = 5, b = 4;
printf("a = %d, and b = %d \n", a, b);
mySwapBit(&a, &b);
printf("Swap by bit. a = %d, and b = %d \n", a, b);
mySwapTwoVaribles(&a, &b);
printf("Swap by two variables a = %d, and b = %d \n", a, b);
}
void mySwapBit(int* a, int* b)
{
if(*a != *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
}
void mySwapTwoVaribles(int* a, int* b)
{
if(*a != *b)
{
*a += *b;
*b = *a - *b;
*a = *a - *b;
}
}
void EvenOrOdd()
{
printf("--- Even number from 1 ~ 100 ----\n");
int i = 0;
for(i = 0; i < 100; i ++)
{
if(IsEven(i))
printf("%d ", i);
}
printf("\n");
printf("--- Odd number from 1 ~ 100 -----\n");
for(i = 0; i < 100; i ++)
{
if(IsOdd(i))
printf("%d ", i);
}
printf("\n");
}
int IsEven(int a)
{
return !(a & 1);
}
int IsOdd(int a)
{
return (a & 1);
}
位操作 示例程序
最新推荐文章于 2021-05-29 21:06:43 发布