A bit compression technique

Source: Internet
Author: User
Determine if a character C is in a string str, what would you do with it? 1.for or while loop can be judged, but the efficiency is 2. Create a static mapping table of size 256 char tb[256], map all the values in the string str to TB, and judge Tb[c]==1 to know if the character exists in Str. Yes, or can be optimized for 3. TB is optimized with bit compression, since each char has 8 bits, the table can be compressed into char TB[256/8]; First compressed str to TB do{tb[*str >> 3] |= 1 << (*str & 7)}while (* (++STR)); What does this code mean? *str >> 3 Right shift three bits equals *str/81 << (*str & 7) to find the value of the last three bits of STR, after bit displacement 0-7 of the value corresponding to the 1-8bit operation is complete, TB each word nonspacing represents 8 characters to be verified, That is, each bit of the character in the TB represents one character of ASCII. Determine if the target bit has been flagged, and C is present in TB. if (Tb[c >> 3] & 1 << (C & 7)) The process of calculation is the same as the compression process, Tb[c >> 3] calculates the corresponding character, 1 << (C & 7) to find out which of the corresponding characters , the two bits are marked with the compressed character. The memory footprint is further optimized through bit operations. 2 and 3 methods have advantages and disadvantages, combined with the scene is very easy to tell which method to use. --Just upgraded the WIN10, all the browser to open the blog Garden Edit Control has an exception, only with IE can edit, but the format is also abnormal, and so normal and then modify the layout.

A bit compression technique

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.