/*
Name: fun_whesponded
Function: determines whether the selected number is in the given integer.
We can know that any natural number can be split into several 2's power and, for example:
1 = 2 ^ 0
2 = 2 ^ 1
3 = 2 ^ 0 + 2 ^ 1
4 = 2 ^ 2
5 = 2 ^ 0 + 2 ^ 2
6 = 2 ^ 1 + 2 ^ 4
7 = 2 ^ 0 + 2 ^ 1 + 2 ^ 2
8 = 2 ^ 3
9 = 2 ^ 0 + 2 ^ 3
10 = 2 ^ 1 + 2 ^ 3
11 = 2 ^ 0 + 2 ^ 1 + 2 ^ 3
12 = 2 ^ 2 + 2 ^ 3
13 = 2 ^ 0 + 2 ^ 2 + 2 ^ 3
14 = 2 ^ 1 + 2 ^ 2 + 2 ^ 3
15 = 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + 2 ^ 3
16 = 2 ^ 4
17 = 2 ^ 0 + 2 ^ 4
Method for resolving any number to the sum of the power of 2-recursion
Rule:
If 14
Limit 2 ^ 3 <14 <2 ^ 4
There must be 8-2 ^ 3 in limit 14
14-8 = 6
Limit 2 ^ 2 <6 <2 ^ 3
IPv6 must have 4--2 ^ 2
6-4 = 2
Limit 2 = 2
Limit 14 = 2 ^ 3 + 2 ^ 2 + 2 ^ 1
Parameters: @ totalnum
Type: int
@ Specifiednum
Type: int
Steps:
Author: waxdoll Cheung
Date: 2005-03-21
*/
Create Function
DBO. fun_whesponded
(
@ Totalnum int,
@ Specifiednum int
)
Returns bit
Begin
Declare @ varret bit
Declare @ varloop int
Set @ varloop = 0
While (@ totalnum> = cast (Power (2, @ varloop) as INT ))
Set @ varloop = @ varloop + 1
Set @ totalnum = @ totalnum-cast (Power (2, @ varloop-1) as INT)
If (@ varloop = @ specifiednum + 1)
Set @ varret = 1
Else
Begin
If (@ totalnum> = 1)
Return DBO. fun_whesponded (@ totalnum, @ specifiednum)
Else
Set @ varret = 0
End
Return @ varret
End
It can be used in many places to see if you can think of it ......