Determine how many digits need to be changed before integer A can be converted to integer B.

Source: Internet
Author: User
1/* determine how many digits need to be changed before integer A can be converted to integer B 2 * Identify the differences between two digits, you can use an exclusive or operation to 3 * view the result of an exclusive or operation, each 1 indicates that the corresponding bits of A and B are not the same 4 *. Therefore, as long as the number of digits is different or the number of digits is 1, you can know that you need to change the number of digits 5*6 **/ 7 public class bitsnum {8 Public int bitswaprequired (int, int B) 9 {10 int COUNT = 0; 11 for (INT c = a ^ B; C! = 0; C = C> 1) 12 {13 count + = C & 1; 14} 15 return count; 16} 17 18 public static void main (string [] ARGs) {19 // todo auto-generated method stub20 bitsnum bit = new bitsnum (); 21 int num = bit. bitswaprequired (2, 4); 22 system. out. println (Num); 23} 24 25}

 

Determine how many digits need to be changed before integer A can be converted to integer B.

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.