Basic things are usually not used much, but will not be used when it comes to touch. Below are some documents
Int type: int32, expressed by four 32-bit bytes, and the height of 31st bits (number starts from scratch) is used to indicate positive and negative, 1 is negative, 0 is positive (left indicates high, right indicates low)
The negative number of the int type is expressed by the complement code, that is, if the positive number of the corresponding int type is reversed by bit, add 1
For example, if-1 is calculated, the positive number is 0000000... 001 (31 zeros and 1) After bitwise inversion, it should be 111111111 .... 10. The result of adding 1 is 11111111... 111 (32 1 ).
The long type is similar to the int type, except that long is represented by 8 bytes and 64 bits.
Common Data ConversionCode
// Convert hexadecimal 0xffffffff to an integer (int32)
MessageBox. Show (convert. toint32 ("ffffffff", 16). tostring (); // display-1
// Convert the binary number 11111111 to an integer (int32). Note that 11111111 is equivalent to 0000... 00011111111 (the first 24 zeros)
MessageBox. Show (convert. toint32 ("11111111", 2). tostring (); // display 255
// Convert integer-1 to a binary string
MessageBox. Show (convert. tostring (-1, 2); // The binary representation of-1, showing 11111 ...... 11 (32 1)
// Convert integer-1 to A hexadecimal string
MessageBox. Show (convert. tostring (-1, 16); // hexadecimal representation of-1, displaying ffffffff
Use bitconverter
Byte [] bytes = new byte [] {0xff, 0xff, 0xff, 0xff, 0, 0 };
// Convert 0th to 3rd elements (four bytes for the int type) of bytes data to an integer.
MessageBox. Show (bitconverter. toint32 (bytes, 0). tostring (); // display-1;
// The byte [] array is represented as a hexadecimal string sequence. Each element is converted and connected with "-". The sequence remains unchanged.
MessageBox. Show (bitconverter. tostring (bytes); // display FF-FF-FF-FF-0-0
// Obtain the byte array of 10 (int32)
Byte [] arr = bitconverter. getbytes (10); // The generated array is {10, 0, 0}
MessageBox. Show (bintconverter. tostring (ARR); // displayed as 0a-0-0-0;
Note that the int32 memory representation of 10 is 00-00-00-0a (the low byte on the left side of the high byte is on the right side), and the above is converted to the high byte through binconverter, and the low byte on the right is on the left side.
Bit operations
When developing some features with permission control, we may use in-place operations,
For example, there are some resources in the system. Each resource corresponds to a group of operations. Assume there are eight operations.
At this time, you can design such a table.
Resource user operation table (user name, resource number, operation type)
Some data:
Tools 1, 1
Tools 2, 2
Tools 1, 3
Alsve 3, 1
Alsve 1, 2
We use numbers 1 ~ 8 indicates eight operations. Obviously, if a user has full permissions on a resource, 8 records are required, if the user Reaches hundreds of thousands, the data volume of this table will be huge.
Use bits to indicate the operation
Resource user operation table (username, resource number, operand)
Here we use the operand, which is an integer of the int type. We use its low 8 bits to indicate that a user has an operation on a resource. If its corresponding bits are 1, the user has the corresponding permission, zero indicates no.
The following operations are required for the above rules:
1. Determine whether a user is 1 or whether the user owns a resource,
Bool expression (operand & (1 <position)> 0;
2. Set a bit to 1 to assign an operation of a resource to the user.
Operand = operand | (1 <position );
3. Set a bit to 0 to cancel a user's operation on a resource.
Operand = operand &(~ (1 <position); // after the shift, perform bitwise inversion, and then perform operations with the operand.
Implementation Details
after a user logs on, the user loads the user's resource operation data to hashtable at one time (including the resource operation data allocated by the user's role)
the code is similar to the following:
foreach (...) {
hash. add ("resource number", "operand");
}< br> of course, the user will belong to some roles, the Resource Operations assigned to the role may overlap with the Resource Operations assigned to the user.
in this case, you need to merge the two, the modified code is similar to the following:
foreach (....) {
If (hash. containskey ("resource number")
{< br> hash ["resource number"] = (INT) hash ["resource number"] | "operand ";
}< br> else
{< br> hash. add ("resource number", "operand");
}< BR >}
Hashtable can be easily serialized. serialized data can be stored in the Session (saved by SQL) or in the encrypted viewstate. Of course, viewstate can also be saved to the SQL server, however, SQL session is more convenient.