Java Note 2 (bitwise operations)

Source: Internet
Author: User

1. Bitwise operators: The use of the ~ operator

public class test22{

public static void Main (string[] args) {

int number1 = 2;

/* Sign bit: 0: Represents the integer 1: Represents a negative number

0000 0000 0000 0000 0000 0000 0000 0010

Reverse: 1111 1111 1111 1111 1111 1111 1111 1101 The inverse is found to be a negative binary.

The binary of negative numbers is converted to decimal: reverse +1 is required.

Complement of negative numbers: Anti-code +1

1000 0000 0000 0000 0000 0000 0000 0010

+ 1

---------------------------------------

0000 0000 0000 0000 0000 0000 0011------(-3)

*/

System.out.println (to); } }

2.

public class test{

public static void Main (string[] args) {

int number = 8;

/*

0000 0000 0000 0000 0000 0000 0000 1000 (Original code)

Reverse 1111 1111 1111 1111 1111 1111 1111 0111 (complement)

Negative original code = negative complement negation +1

1000 0000 0000 0000 0000 0000 0000 1000 (Complement reverse)

+ 1

---------------------------------------

0000 0000 0000 0000 0000 0000 1001---"-9 * *

System.out.println (to); } }

3. Use of logical operators
public class luojitest{
public static void Main (string[] args) {
int number1 = 20;
int number2 = 30;
--&&: Short Circuit and
--(Number1>number2): False (NUMBER1>10): True
False&&true
System.out.println ("&&: Short circuit with:" + ((number1>number2) && (number1>10)));
--| |: Short Circuit or
false| | True
System.out.println ("| |: Short circuit OR:" + ((number1>number2) | | (number1>10)));
--!: Non-operator
Number1>number2-> 20>30--false
System.out.println ("!: Non-operator:" + (!) ( (Number1>number2)));
--&: Single and
System.out.println ("&: Single and" + ((number1>number2) & (NUMBER1>10)));
--|: Single or
System.out.println ("|: Single or" + ((number1>number2) | ( (number1>10)));
--^: XOR or
(Number1>number2): False (NUMBER1>10): True
False^true
System.out.println ("^ xor:" + ((number1>number2) ^ (number1>10)));
System.out.println (true^true);
System.out.println (False^false);
}
}

4. Implement a three-bit integer input from the keyboard and reverse output

Import Java.util.Scanner;
public class homeworktest{
public static void Main (string[] args) {

Scanner scan = new Scanner (system.in);
int number = Scan.nextint ();

int baiwei = number/100;
int Shiwei = NUMBER%100/10;
int gewei = number%100%10;
SYSTEM.OUT.PRINTLN ("Starting data:" +number);
System.out.println ("After conversion:" +gewei+ "" +shiwei+ "" +baiwei ");

}
}

Java Note 2 (bitwise operations)

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.