Store Data Using int Data shift

Source: Internet
Author: User

I have learned how to shift a number before, but I have never used it. During this time, I spent the whole day with several C Programs and learned a lot of good things. The following figure shows that they use the shift operation to store some data ~~

The problem is that a player and a series of item IDS (, 3...) in his package are known ...), according to the previous thinking, of course, there is a gamer item table that stores the player ID and item ID, but C Programs store it like this:

If you want to save the item IDs 1, 2, 3, consider these three IDs as 1110, that is, using 0 and 1 to indicate whether there is a value in this position (from right to left ), 1110 means that there are values at positions 1 and 2 and 3 respectively. In this way, the three ID values can be saved with one data, and 1110 is the decimal number 14 (2 + 4 + 8 ). After reading the data from the database, you can use the shift operation to resolve 14 to 1110, and it is very convenient to generate a gamer package with the number of 1110. The following is the sample code, for example, to save the 4 data: 2, 3, 4, and 7:

Public class maintest {public static void main (string [] ARGs) {int COUNT = 10; // assume that the maximum ID is 10 int [] arr = }; int A = getint (ARR); // you can use the shift operation to save four numbers into a number of systems. out. println ("A =" + a); // The following is the parsing method // ------------ method 1 through shift resolution ---------------- int [] arr2 = new int [10]; for (INT I = 0; I <count; I ++) {arr2 [I] = (A & (1 <I)> I; // tell the truth, I didn't understand it, but the result was correct. C programmer said: You can understand it when you write more .... } System. out. print ("str1 ="); For (INT I = 0; I <arr2.length; I ++) {system. out. print (arr2 [I] + ",");} system. out. println (); // --------- method 2. the method that comes with Java can be understood through the method name ------------------- string STR = integer. tobinarystring (a); system. out. println ("str2 =" + Str);} public static int getint (int... ARR) {int A = 0; For (INT I = 0; I <arr. length; I ++) {A | = (1 <arr [I]); // 1 <a value shifts 1 left and adds 0 to the right, then, set the number a | = to 1 for the binary number of A. This method can be used for update. If it is already 1, if the value is 0, the update value is 1. You can add a slight judgment to know which ID data is added and which IDs are existing} return ;}}

 

 

Run the program. Result:

a = 14str1 = 0,1,1,1,0,0,0,0,0,0,str2 = 1110

 

Of course, pay attention to the int value range. If the number of item IDs exceeds 32 bits, it cannot be saved as this way. Of course, you can add an item type judgment. Generally, the same type of things in the game will not exceed this number ..

Learn more about C code in the future ~~

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.