File tree Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Original file line number Diff line number Diff line change 66 * @Description:
77 * 无符号数a的左移相当于将该数用二进制表示,左移n位就是把最高位n位移出,低位添加n个0的操作,左移操作相当于将该数乘以2^n次方。
88 * 右移操作相当于将该数除以2^n次方。
9+ *
10+ *
11+ * 位运算符先用二进制表示,再对每一位进行计算。
12+ * &表示同时不为0,才是1。
13+ * |表示只要有一个为1,就是1。
14+ * ^表示相同则为0,不同则为1.
15+ * ~表示取反。
16+ *
917 */
1018public class BitOperation {
1119 public static void main (String [] args ) {
20+ System .out .println ("位运算符:" );
21+ // 16用二进制表示是 10000,右移四位得到 1.
1222 System .out .println ("1左移4位结果为:" +(1 <<4 ) );
23+ System .out .println ("16右移4位结果为:" +(16 >>4 ) );
1324
14- System .out .println ("1左移4位结果为:" +(16 >>4 ) );
25+ //2用二进制表示是10,而4用二进制表示是100.运算时,位数不足就补0.
26+ // 010 & 100 , 对每一位进行运算, 结果得到 000
27+ System .out .println ("2&4结果为:" +(2 &4 ));
28+ // 010 | 100 , 对每一位进行运算, 结果得到 110
29+ System .out .println ("2|4结果为:" +(2 |4 ));
30+ // 010 ^ 100 , 对每一位进行运算, 结果得到 110
31+ System .out .println ("2^4结果为:" +(2 ^4 ));
32+ // 非运算的结果比较复杂,涉及到"相反数"
33+ System .out .println ("~2结果为:" +(~2 ));
1534
1635 }
1736}
Original file line number Diff line number Diff line change 1+ package com .string ;
2+
3+ import com .alibaba .druid .sql .visitor .functions .Char ;
4+
5+ /**
6+ * @Author: lenovo
7+ * @Date: 2020/2/1 21:39
8+ * @Description:
9+ */
10+ public class CharSwapDemo {
11+
12+ public static String swapCharInString (String str ) {
13+ if (str == null ) {
14+ return null ;
15+ }
16+ //注意,要给StringBuilder初始化,否则后续设置字符会报错"数组越界"
17+ StringBuilder sb = new StringBuilder (str );
18+ int length = str .length ();
19+ for (int i = 0 ; i < length ; i ++) {
20+ sb .setCharAt (i , str .charAt (length - i - 1 ));
21+ }
22+ return sb .toString ();
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1+ package com .string ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static org .junit .jupiter .api .Assertions .*;
6+
7+ /**
8+ * @Author: lenovo
9+ * @Date: 2020/2/1 21:46
10+ * @Description:
11+ */
12+ class CharSwapDemoTest {
13+
14+ @ Test
15+ void swapCharInString () {
16+ String s = CharSwapDemo .swapCharInString ("abcd" );
17+ System .out .println (s );
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments