Common bit operations: Get, set, and clear

Source: Internet
Author: User
1/* Common bit operations: Get, set, zeroes 2*3 **/4 public class bitget {5/* 6 * This function provides the retrieval function. 7 * First, 1 is moved to the I bit, and 0 is shifted to 00000001, 8 * and then convert it to binary 00001100 with num decimal 12, 9*00000001 if it is to move two digits to the left, it will be 0000010010*00001100 0000110011 00000000 * -------- 12*0000010013 *, so as to clear all the bits except the I bit. Finally, check whether the result is zero. If 14 * is not zero, it indicates that the I-th bit is 1. Otherwise, it is zero 15 */16 public Boolean getbit (INT num, int I) 17 {18 return (Num & (1 <I ))! = 0); 19} 20/* This function implements the slot function 21 * First Move 1 to the left of the I bit to get the value in the shape of 00001000. Then, bitwise OR 22 * For this value and num will only change the I-th value, the binary code of num other bits 23 * as follows 24*12 is 0000110025 * If 1st bits are set to 26*0000110027*0000001028 * -------- 29*0000111030 *, the second position is 1, the rest will remain unchanged 31 **/32 public int setbit (INT num, int I) 33 {34 return num | (1 <I ); 35} 36/* 37 * This function implements the zeroth function 38 * This method is just the opposite of setbit. First, Move 1 to the left of the I bit, then obtain the mask 39 * For this value, and then perform bitwise AND operation on the mask and num. This will only reset the I bit of num, the remaining BITs remain unchanged 40 * as follows 4 The binary value of 1*12 is 0000110042 * assume that the number of digits to be cleared is 43*1. move 1 to the left and get 0000100044*2. returns 1111011145x3. bitwise and operation 46*1111011147*0000110048 * -------- 49*0000010050 * can be obtained, only the third bit is cleared, and other values remain unchanged 51 **/52 public int clearbit (INT num, int I) 53 {54 int mask = ~ (1 <I); 55 return num & mask; 56} 57 public static void main (string [] ARGs) {58 // todo auto-generated method stub59 bitget BG = new bitget (); 60 Boolean flag = BG. getbit (12, 3); 61 system. out. println (FLAG); 62 int value = BG. setbit (12, 1); 63 system. out. println ("1st bits:" + value); 64 value = BG. clearbit (12, 3); 65 system. out. println ("Reset 3rd bits:" + value); 66 67} 68 69}

 

Common bit operations: Get, set, and clear

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.