• 【Java SE]位运算和移位运算需要知道的重点


    目录

    1.位运算

    1.1按位与 &:

    如果两个二进制位都是 1, 则结果为 1, 否则结果为 0

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Test {
    2. public static void main(String[] args) {
    3. int a=10;
    4. int b=1;
    5. int c=a&b;
    6. System.out.println(c);
    7. }
    8. }
  • 1.2按位或 |:

    如果两个二进制位都是 0, 则结果为 0, 否则结果为 1

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Test {
    2. public static void main(String[] args) {
    3. int a=10;
    4. int b=1;
    5. int c=a|b;
    6. System.out.println(c);
    7. }
    8. }

1.3按位取反 ~:

如果该位为 0 则转为 1, 如果该位为 1 则转为 0

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Test {
  2. public static void main(String[] args) {
  3. int a=10;
  4. a=~a;
  5. System.out.println(a);
  6. }
  7. }

1.4按位异或 ^:

如果两个数字的二进制位相同, 则结果为 0, 相异则结果为 1

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Test {
  2. public static void main(String[] args) {
  3. int a=10;
  4. int b=1;
  5. int c=a^b;
  6. System.out.println(c);
  7. }
  8. }

2.移位运算

2.1 左移 <<:

最左侧位不要了, 最右侧补 0

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Test {
  2. public static void main(String[] args) {
  3. int a=10;
  4. int b=a<<1;
  5. System.out.println(b);
  6. }
  7. }

2.2右移 >>:

最右侧位不要了, 最左侧补符号位(正数补0, 负数补1)

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Test {
  2. public static void main(String[] args) {
  3. int a=10;
  4. int b=a>>1;
  5. System.out.println(b);
  6. }
  7. }

2.3无符号右移 >>>:

最右侧位不要了, 最左侧补 0.

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Test {
  2. public static void main(String[] args) {
  3. System.out.println(1>>>1);
  4. System.out.println(-1>>>1);
  5. }
  6. }

  • 相关阅读:
    第十八章:Swing自述
    设置github的默认分支
    IPIDEA代理IP如何帮助企业采集市场信息
    uniapp图片base64保存本地
    C#单向链表实现非升序插入方法:用LinkedList<int>链表类 vs 自定义单向链表类及非升序插入方法
    python绘制三维图
    基于51单片机的温度控制恒温箱设计
    近日找实习总结
    MySQL:Update高并发下变慢的案例及其涉及的特性
    用Rust写个猜数字的游戏(详解)
  • 原文地址:https://blog.csdn.net/weixin_62421895/article/details/126033123