美文网首页
二进制掩码 (Binary Mask)

二进制掩码 (Binary Mask)

作者: Tinyspot | 来源:发表于2024-02-01 16:16 被阅读0次

1. 二进制掩码

  • 通过按位逻辑运算(如 AND、OR 或 NOT),可以实现对目标数据特定位置比特的选择、设置或清除

1.1 进制转换

@Test
public void toBinary() {
    long value = (long) Math.pow(2, 2);
    System.out.println(Long.toBinaryString(value));
}

@Test
public void toData() {
    String binary = "110";
    int data = Integer.parseInt(binary, 2);
    int data2 = Integer.valueOf(binary, 2);
    System.out.println(data + "; " + data2);
}

2. 选择特定位 (按位与 &)

  • 用掩码选择字节中的特定位
数据  101
掩码  100
结果  100

示例:

public enum AbnormalFlagEnum {
    ABNORMAL_ZERO((long) Math.pow(2, 0), "二进制:1"),
    ABNORMAL_ONE((long) Math.pow(2, 1), "二进制:10"),
    ABNORMAL_TWO((long) Math.pow(2, 2), "二进制:100"),
    ;

    private Long code;
    private String desc;

    AbnormalFlagEnum(Long code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public Long getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public static AbnormalFlagEnum getByCode(String code) {
        if (code == null) {
            return null;
        }
        for (AbnormalFlagEnum value : values()) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        return null;
    }
}
@Test
public void mask() {
    long abnormalFlag = 5;
    long mask = AbnormalFlagEnum.ABNORMAL_TWO.getCode();
    System.out.println(Long.toBinaryString(abnormalFlag) + " & " + Long.toBinaryString(mask) + " = "
            + Long.toBinaryString(abnormalFlag & mask));
    System.out.println((abnormalFlag & mask) == mask);
    System.out.println((abnormalFlag & mask) > 0);
}

打印结果:

101 & 100 = 100
true
true

3. 设置特定位 (按位或 |)

数据  101
掩码  100
结果  101

示例:

@Test
public void calBit() {
    long source = (long) Math.pow(2, 2);
    source |= 5;
    source |= 6;
    System.out.println(Long.toBinaryString(source));
}

4. 清除特定位 (按位异或 ^)

数据  1100
掩码   100
结果  1000

示例:

@Test
public void clearBit() {
    long mask = (long) Math.pow(2, 2);
    int binary = Integer.parseInt("1100", 2);

    binary ^= mask;
    System.out.println(Long.toBinaryString(binary));
}

相关文章

  • 5、变长子网掩码(VLASM)/汇总和TCP/IP故障排除——C

    5.1 变长子网掩码(VLSM) VLSM(Variable Length Subnet Mask,变长子网掩码)...

  • 子网掩码?DNS/DHCP

    先来瞧瞧这些玩意都在哪 子网掩码 子网掩码(subnet mask)又叫网络掩码、地址掩码、子网络遮罩,它是一种用...

  • 子网掩码,ip地址,网关

    一,子网掩码 定义 子网掩码(subnet mask)又叫网络掩码、地址掩码、子网络遮罩,它是一种用来指明一个IP...

  • 子网掩码

    子网掩码:subnet mask, 又叫网络掩码,地址掩码,子网络遮罩,它是一种用来指明哪些位标识的是主机所在的子...

  • Binary Mask

    " Mask operation is used to hide object either partially ...

  • 网络词语的基本概念

    网络概念扫盲帖 子网掩玛子网掩码(subnet mask)又叫网络掩码、地址掩码、子网络遮罩,它是一种用来指明一个...

  • 掩码操作

    掩码(英语:Mask)在计算机学科及数字逻辑中指的是一串二进制数字,通过与目标数字的按位操作,达到屏蔽指定位而实现...

  • 子网掩码

    子网掩码(subnet mask)又叫网络掩码、地址掩码、子网络遮罩,它是一种用来指明一个IP地址的哪些位标识的是...

  • 90-73子网掩码

    子网掩码(subnet mask)又叫网络掩码、地址掩码、子网络遮罩,它是一种用来指明一个IP地址的哪些位标识的是...

  • 尝试使用位掩码Mmm...mask!

    说到mask掩码你会想到什么?子网掩码?没错,那也是掩码,但实际上掩码技巧在我们平时的应用中也可以让你有意想不到的...

网友评论

      本文标题:二进制掩码 (Binary Mask)

      本文链接:https://www.haomeiwen.com/subject/vcrzodtx.html