Java 100 Days Programming Guide - Day 5: Operators and Expressions (Part 2)

7cf03a82b57c4b139f3a15fa463eab12.gif


content

I. Introduction

2. Logical operators

1. with

2. or

3. Not

Third, the ternary operator

4. Basic bit operations

1. with

2. or

3. Not

4. Same or

5. XOR

Five, shift operation

1. Shift left

2. Move right

6. Priority


I. Introduction

  • Learning Objective 1: Familiarize yourself with basic logical operators, ternary operator calculations
  • Learning Objective 2: Be proficient in applying bitwise operations and, OR, NOT, XOR, XOR, and shift operations. Bit operations will appear in many interview questions, and you need to understand the final priority table.

2. Logical operators

  • Definition : Used to perform operations on logical data, namely AND, OR, NOT.

1. with

public static void main(String[] args)
	{
		int i,j,k;//与运算
		i=10;
		j=20;
		k=30;
		//有false则false
		System.out.println((i>j)&&(i>k));//false&&false   false
		System.out.println((i>j)&&(i<k));//false&&true    false   
		System.out.println((i<j)&&(i>k));//true&&false    false
		System.out.println((i<j)&&(i<k));//true&&true     true
	}
  • If the previous one is false, the latter one is not executed, and false is directly output 

2. or

	public static void main(String[] args)
	{
		int i,j,k;//或运算
		i=10;
		j=20;
		k=30;
		//有true则true
		System.out.println((i>j)||(i>k));//false||false   false
		System.out.println((i>j)||(i<k));//false||true    true   
		System.out.println((i<j)||(i>k));//true||false    true
		System.out.println((i<j)||(i<k));//true||true     true
	}
  •  If the former is true, the latter will not be executed, and output true directly 

3. Not

public static void main(String[] args)
	{
		int i,j,k;//非运算
		i=10;
		j=20;
		k=30;
		System.out.println((i>j));//false
		System.out.println(!(i>j));//true
		System.out.println(!!(i>j));//false
	}

Summary :

symbol introduce illustrate
&& and true&&true=true
|| or true||false=true
! No Negative operation, !false=true !true=false

Third, the ternary operator

  • Format : conditional expression ? value1 : value2
  • Execution process : first calculate the value of the expression , if the value is true , return value 1 , if it is false , return value 2

example:

  • Given the values ​​of a and b, find the maximum value and assign it to max
public static void main(String[] args)
	{
		int a,b,max;//初始化数据变量
		a=20;//赋值
		b=10;//赋值
		max=a>b?a:b;//max等于a b两者中间较大的一个
		System.out.println(max);//输出结果
	}
//输出20

4. Basic bit operations

1. with

  • Symbol : &
  • Operation rules : 1 when both binary bits are 1, otherwise 0
  • Example : 1001&1111=1001

2. or

  • Symbol : |
  • Operation rules : 0 when both binary bits are 0, otherwise 1
  • Example : 1001&1100=1101

3. Not

  • Symbol : ~
  • Operation rules : the first sign bit remains unchanged, 0 becomes 1, 1 becomes 0, and the last bit +1
  • Example : ~1001 = 0110

4. Same or

  • Symbol : ~
  • Operation rules : 1 when the numbers are the same, 0 on the contrary
  • Example : 1001~1100=1010

5. XOR

  • Symbol : ^
  • Operation rules : the opposite of two binary bits is 1, the same is 0
  • Example : 1001^0111=1110

Five, shift operation

1. Shift left

  • Symbol : <<
  • Operation rules : the sign bit remains unchanged, and the low bit is filled with 0

Example :

a<<b 代表十进制数字a向左移动b个进位
/* 左移:
 * 左移1位,相当于原数值 * 2
 * 左移2位,相当于原数值 * 4
 * 左移n位,相当于原数值 * 2^n
 */
计算 10 << 1
10的补码:0000 1010
-----------------------
结果补码:0001 0100 ==> 正数,即 10*2=20

计算 10 << 2
10的补码:0000 1010
-----------------------
结果补码:0010 1000 ==> 正数,即 10*2^2=40

计算 10 << 3
10的补码:0000 1010
-----------------------
结果补码:0101 0000 ==> 正数,即 10*2^3=80

计算 12 << 1
12的补码:0000 1100
-----------------------
结果补码:0001 1000 ==> 正数,即 12*2=24

2. Move right

  • Symbol : >>
  • Operation rules : overflow from the low bit, the sign bit remains unchanged, and fill the high bit of the overflow with the sign bit

Example :

a>>b 代表十进制数字a向右移动b个进位
/* 右移:
 * 右移1位,相当于原数值 / 2
 * 右移2位,相当于原数值 / 4
 * 右移3位,相当于原数值 / 2^n
 * 结果没有小数(向下取整)
 */
计算 80 >> 1
80的补码:0101 0000
-----------------------
结果补码:0010 1000 ==> 正数,即 80/2=40

计算 80 >> 2
80的补码:0101 0000
-----------------------
结果补码:0001 01000 ==> 正数,即 80/2^2=20

计算 80 >> 3
80的补码:0101 0000
-----------------------
结果补码:0000 1010 ==> 正数,即 80/2^3=10

计算 24 >> 1
12的补码:0001 1000
-----------------------
结果补码:0000 1100 ==> 正数,即 24/2=12

6. Priority

operator describe priority
[]、() array, parentheses 1
++ -- ! ~ self-increment, self-decrement 2
* / % Multiplication and division, remainder 3
+ - plus, minus 4
<< 、 >>  、>>> shift operation 5
<  、<=、  > 、>= relational operations 6
==、 != relational operations 7
& bit and 8
^ XOR 9
| bit or 10
&& logical and 11
|| logical or 12
?: Ternary operator 13
= 、*=、 /=、 +=、 -=、 ^= assignment operator 14
&=、 |=、 <<=、 >>= 、>>>= assignment operator 15

Guess you like

Origin blog.csdn.net/qq_53673551/article/details/123954091