原文链接: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
;
}