Bitwise Operation Explained

简介:
原文链接:Bitwise Operation Explained

1.统计一个数置位为1的个数

复制代码
#include <stdio.h>
int __numOf_SET_Bits(int var)
{
    if (var==0) return 0;
    else return (var&01)?1+__numOf_SET_Bits(var>>1):__numOf_SET_Bits(var>>1);
}
int main()
{
    int var=128;
    printf("Num of Bits: %d\n",__numOf_SET_Bits(var));
    return 0;
}
复制代码
2,判断一个数是奇数还是偶数

复制代码
#include <stdio.h>
#define isEven(a) ((((a)&01)==0)?1:0)
int main()
{
    int var=1;
    if(isEven(var))
    {
        printf("%d is a even number \n",var);
    }
    else
        printf("%d is a odd number \n",var);
    return 0;
}
复制代码
方法二:

复制代码
#include <stdio.h>
#define isEven(a) ((((a)%2)==0)?1:0)
int main()
{
    int var=11;
    if(isEven(var))
    {
        printf("%d is a even number \n",var);
    }
    else
        printf("%d is a odd number \n",var);
    return 0;
}
复制代码
3,判断一个数是否是2的幂次方

复制代码
#include <stdio.h>
#define __isPower_of_TWO(a) (((a)&(a-1))==0)?1:0
int main()
{
    int arr[] = {1,2,3,4,5,8,9,16};
    int i=0;
    for(;i<sizeof(arr)/sizeof(arr[0]);i++)
    {
        if (__isPower_of_TWO(*(arr+i)))
            printf("%d has a form of Power of Two \n",*(arr+i));
        else
            printf("%d is not in the form \n", *(arr+i));
    }
    return 0;
}
复制代码
方法二:

复制代码
#include <stdio.h>
#define __isPower_of_TWO(a) (((a)&(-a))==a)?1:0
int main()
{
    int arr[] = {1,2,3,4,5,8,9,16};
    int i=0;
    for(;i<sizeof(arr)/sizeof(arr[0]);i++)
    {
        if (__isPower_of_TWO(*(arr+i)))
            printf("%d has a form of Power of Two \n",*(arr+i));
        else
            printf("%d is not in the form \n", *(arr+i));
    }
    return 0;
}
复制代码
方法三:

 复制代码
#include <stdio.h>
#include <stdlib.h>
int __numOf_SET_Bits(int var)
{
    if (var==0) return 0;
    else return (var&01)?1+__numOf_SET_Bits(var>>1):__numOf_SET_Bits(var>>1);
}

int main()
{
    int arr[] = {1,2,3,4,5,8,9,16};
    int i=0;
    for(;i<sizeof(arr)/sizeof(arr[0]);i++)
    {
        if (__numOf_SET_Bits(arr[i])==1)
            printf("%d has a form of Power of Two \n",*(arr+i));
        else
            printf("%d is not in the form \n", *(arr+i));
    }
    system("pause");
    return 0;
}
复制代码
4,不使用第三个数,交换两个数

复制代码
#include <stdio.h>
void __SWAP(int *a,int *b)
{
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
}
int main()
{
    int a=5, b=6;
    printf("Before swap: a=%d <=====> b=%d \n",a,b);
    __SWAP(&a,&b);
    printf("After  swap: a=%d <=====> b=%d \n",a,b);
    return 0;
}
复制代码
5,异或双向链表

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct XOR_based_Node 
{
    int data;//数据域
    unsigned long compressedAddress;
}node;
node* head = NULL;//异或双向链表表头
void add_element_to_list(node** headRef, int data)
{//插入表头
    node *newNode = (node*)malloc(sizeof(node));
    assert(newNode);
    newNode->compressedAddress = (unsigned long)(*headRef);
    newNode->data = data;
    if(*headRef != NULL)
        (*headRef)->compressedAddress ^= (unsigned long)newNode;
    *headRef=newNode;
}
void printList(node* head)
{//输出表
    unsigned long prev = 0;
    while(head)
    {
        unsigned long next = prev ^ head->compressedAddress;
        printf("%d ", head->data);
        prev = (unsigned long)head;
        head = (node *)next;
    }
    printf("\n");
}
int main(void) 
{
    int i=0;
    for(;i<10;i++)
        add_element_to_list(&head,i);
    printList(head);
    return 0;
}

复制代码



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/08/22/1273784.html,如需转载请自行联系原作者
目录
相关文章
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
2521 0
|
6月前
|
人工智能
Big Water Problem
Big Water Problem
37 0
|
算法 Linux Shell
SGAT丨Single Gene Analysis Tool
SGAT丨Single Gene Analysis Tool
|
自然语言处理
Reading the Manual: Event Extraction as Definition Comprehension, EMNLP 2020
Reading the Manual: Event Extraction as Definition Comprehension, EMNLP 2020
89 0
Reading the Manual: Event Extraction as Definition Comprehension, EMNLP 2020
|
机器学习/深度学习 计算机视觉
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
145 0
|
SQL 移动开发 算法
New Dynamic Programming Algorithm for the Generation of Optimal Bushy Join Trees
MySQL无疑是现在开源关系型数据库系统的霸主,在DBEngine的最新排名中仍然稳居第2位,与第3位SQL Server的积分差距并不算小,可以说是最受欢迎,使用度最高的数据库系统,这一点看看有多少新型数据库要兼容MySQL的协议和语法就知道了。
319 0
New Dynamic Programming Algorithm for the Generation of Optimal Bushy Join Trees