In SQL Server, and the like may be employed 1,2,4,8,16 ..... accumulate a state of a digital ID field, the presence of several states may be combined to form various combinations of state may be
For example: a field number in the original record, 2, 4 as we would like to add, can be used
update t_User set iFlag = iFlag | 4 where UserID = 1
(IFlag for the field name)
Example 2: After adding 4 we want to get rid of 4 how to do it, it can be achieved
update t_User set iFlag = iFlag ^4 where UserID = 1
This 4 again removed from the record.
If we want to select all records for the 2 of how to do it, it can be achieved
select * from t_User where iFlag &2 = 2
Bit computing in SQL can not only remove the various values, and we can sort the data on his
For example the following, a field identifies the news list as
1: Top
2: Not Top
4: Recommended
8: not recommended
The value of this field can be a combination of these four states, and if we want to behave in front of all the top of it according to certain conditions
select * from t_News order by iFlag & 1 desc
So we put all the top posts in the top surface, of course, where you can add Where certain conditions, Where there may be added a certain bit computing,
Bit operation can be found on the corresponding SQL Help
Let's enumerate Bitwise talk in C #
Here we define an enumeration
[Flags]
enum UserFlag
{
a = 1,
b = 2,
c = 4,
d = 8,
e = 16,
f = 32
}
In the process, add the following code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
It is first written: string strSQL = "select * from v_User where iFlag & @iFlag = @iFlag";
This is the second wording: string strSQL = "select * from v_User where @iFlag | iFlag = iFlag
//SqlParameter parm = new SqlParameter("@iFlag",SqlDbType.Int,4);
//parm.Value = UserFlag.a | UserFlag.b ;
SqlConnection con = new SqlConnection("server=.;database=Sinvan_TexDB;User Id=sa;pwd=123;");
SqlCommand comm = new SqlCommand(strSQL, con);
comm.Parameters.Add("@iFlag", SqlDbType.Int, 4).Value = UserFlag.a | UserFlag.b;
SqlDataAdapter adp = new SqlDataAdapter(comm);
DataTable dTable = new DataTable();
adp.Fill(dTable);
UserFlag userFlag = (UserFlag)Enum.Parse(typeof(UserFlag), dTable.Rows[0][11].ToString());
}
}
After treatment userFlag is present in various combinations in the database
We also may be a certain bit arithmetic processing
If we want to add UserFlag.c proceed as follows
userFlag = userFlag | Userflag.c
To remove UserFlag.c proceed as follows
userFlag = userFlag ^ UserFlag.c
As we have to determine whether or not there is the identifier c may be carried out as follows
(userFlag & UserFlag.c) == UserFlag.c
SQL Server is not in operation and similar, no matter what language bit computing are common, huh
Unfortunately, Access does not support bit computing
select 2|8 --10
select 2|8|1 --11
select 10 & 8 --8, comprising, 10 =. 8 + 2
SELECT 10 & 2 --2, comprising, 10 = 2 +. 8
SELECT 10 &. 4 --0, does not include
select 19 & 16 --16, comprising,. 19 = 16 + 2 +. 1
SELECT 8--0 & 19, comprising, 19 = 16 + 2 + 1
select * from SqlBitOperation where FBitTags&4=4
In the application of rights
1, two tables
(1), operating with permission flags table
if exists (select * from sysobjects where id = OBJECT_ID('[RightTags]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [RightTags]
CREATE TABLE [RightTags] (
[Pkid] [int] IDENTITY (1, 1) NOT NULL,
[RightsName] [nvarchar] (50) NOT NULL,
[RightsTag] [int] NOT NULL DEFAULT (0))
ALTER TABLE [RightTags] WITH NOCHECK ADD CONSTRAINT [PK_RightTags] PRIMARY KEY NONCLUSTERED ( [Pkid] )
SET IDENTITY_INSERT [RightTags] ON
INSERT [RightTags] ([Pkid],[RightsName],[RightsTag]) VALUES ( 1,'吃饭',1)
INSERT [RightTags] ([Pkid],[RightsName],[RightsTag]) VALUES ( 2,'拉屎',2)
INSERT [RightTags] ([Pkid],[RightsName],[RightsTag]) VALUES ( 3,'打炮',4)
INSERT [RightTags] ([Pkid],[RightsName],[RightsTag]) VALUES ( 4,'泡妞',8)
INSERT [RightTags] ([Pkid],[RightsName],[RightsTag]) VALUES ( 5,'生子',16)
INSERT [RightTags] ([Pkid],[RightsName],[RightsTag]) VALUES ( 6,'包二奶',32)
SET IDENTITY_INSERT [RightTags] OFF
(2), and user permissions table
if exists (select * from sysobjects where id = OBJECT_ID('[RightUsers]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [RightUsers]
CREATE TABLE [RightUsers] (
[Pkid] [int] IDENTITY (1, 1) NOT NULL,
[FUser] [nvarchar] (50) NOT NULL,
[UserRights] [int] NOT NULL DEFAULT (0))
ALTER TABLE [RightUsers] WITH NOCHECK ADD CONSTRAINT [PK_RightUsers] PRIMARY KEY NONCLUSTERED ( [Pkid] )
SET IDENTITY_INSERT [RightUsers] ON
INSERT [RightUsers] ([Pkid],[FUser],[UserRights]) VALUES ( 1,'张三',7)
INSERT [RightUsers] ([Pkid],[FUser],[UserRights]) VALUES ( 2,'李四',9)
INSERT [RightUsers] ([Pkid],[FUser],[UserRights]) VALUES ( 3,'王老板',63)
INSERT [RightUsers] ([Pkid],[FUser],[UserRights]) VALUES ( 4,'朱麻子',0)
SET IDENTITY_INSERT [RightUsers] OFF
2, to determine whether there is access permissions
- 1. Query permissions
DECLARE @iRights AS int
the SELECT @ iRights = (1 RightsTag from the SELECT Top RightTags the WHERE RightsName = 'shelling')
--print @iRights
the SELECT * from RightUsers the WHERE Userrights iRights & @ = @ iRights - all can shelling people
select * from RightUsers where FUser = 'John Doe' and UserRights & @ iRights = @ iRights - whether the person can shoot a cannon (whether the record set)
select * from RightUsers where FUser = 'Wong' and UserRights & @ iRights = @ iRights - this person can shoot a cannon (whether the record set)
go
3, set permissions
- 2. Set permissions
DECLARE @yourRights int
the SELECT @ yourRights = (the SELECT SUM (RightsTag) from RightTags in the WHERE RightsName ( 'eat', 'shit', 'shelling'))
--print @yourRights
update RightUsers set UserRights=@yourRights where FUser='张三'
go
4, view all permissions for this person
- 3. Check all human rights
DECLARE @yourRights AS int
the SELECT @ yourRights = (RightUsers the WHERE from the SELECT Userrights FUSER = 'John Doe')
Print @yourRights
select * from RightTags where @yourRights&RightsTag=RightsTag
go
5, detection permission table value is an integer power authority 2 (effectiveness of the operation permission flag: a unique integer power of +2)
select power(2,6) --64
select power(2.000,5.500) --45.255
select log(64.000)/log(2.000)--6.0
------------------------------
DECLARE @setNewOperateRightTags int - add a new operating authority
set @ setNewOperateRightTags = 64 --64 is a power of two
--set @ setNewOperateRightTags = integer power 63--63 than 2
DECLARE @number a float
SET @ Number = log (@setNewOperateRightTags) / log (a 2.000)
--print @number
IF (ceiling (@number) = Floor (@number)) - taking the maximum integer = minimum integer takes
the begin
Print Cast ( @setNewOperateRightTags as varchar (16)) + ' is an integer power of two, it may be used as permission flag value'
End
the else
the begin
Print Cast (@setNewOperateRightTags as varchar (16)) + ' is not an integer power of 2, can not be used permission flag value '
End
Reproduced in: https: //my.oschina.net/cjkall/blog/195907