java bitset_Java BitSet flip()方法与示例

java bitset

BitSet类flip()方法 (BitSet Class flip() method)

Syntax:

句法:

    public void flip(int bit_in);
    public void flip(int st_in, int en_in);

  • flip() method is available in java.util package.

    flip()方法在java.util包中可用。

  • flip(int bit_in) method is used to set the bit at the given bit index to the reverse of its current set value.

    flip(int bit_in)方法用于将给定位索引处的位设置为其当前设置值的反方向。

  • flip(int st_in, int en_in) method is used to set each bit in a range at the given st_in(starting index) and en_in(ending index) to the reverse of its current set value.

    flip(int st_in,int en_in)方法用于将给定st_in(起始索引)和en_in(结束索引)的范围内的每个位设置为其当前设置值的倒数 。

  • flip(int bit_in) method may throw an exception at the time of assigning index.

    flip(int bit_in)方法在分配索引时可能会引发异常。

    IndexOutOfBoundsException: This exception may throw when the given index is not in a range.

    IndexOutOfBoundsException :如果给定索引不在范围内,则可能引发此异常。

  • flip(int st_in, int en_in) method may throw an exception at the time of assigning index.

    flip(int st_in,int en_in)方法在分配索引时可能会引发异常。

    IndexOutOfBoundsException: This exception may throw when st_in, en_in are less than 0 or st_in > en_in.

    IndexOutOfBoundsException :如果st_in , en_in小于0或st_in> en_in ,则可能引发此异常。

  • These are non-static methods, so it is accessible with the class object and if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,因此可以通过类对象进行访问,如果尝试使用类名称访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the second case, flip(int bit_in)

    在第二种情况下, flip(int bit_in)

    • int bit_in – represents the index of the bit in this BitSet to flip.
    • int bit_in –表示此BitSet中要翻转的位的索引。
  • In the third case, flip(int st_in, int en_in)

    在第三种情况下, flip(int st_in,int en_in)

    • int st_in – represent the index of the starting bit to flip.
    • int st_in –表示要翻转的起始位的索引。
    • int en_in – represents the index after the ending bit to flip.
    • int en_in –表示要翻转的结束位之后的索引。

Return value:

返回值:

In all the cases, the return type of the method is void, it returns nothing.

在所有情况下,该方法的返回类型都是void ,它什么也不返回。

Example:

例:

// Java Program to demonstrate the example of
// void flip() method of BitSet

import java.util.*;

public class Flip {
    public static void main(String[] args) {
        // create an object of BitSet
        BitSet bs = new BitSet(10);

        // By using set() method is to set
        // the values in BitSet 
        bs.set(10);
        bs.set(20);
        bs.set(30);
        bs.set(40);
        bs.set(50);

        // Display Bitset
        System.out.println("bs: " + bs);

        // By using flip(int bit) method is to
        // flip the matches bit from bitset
        bs.flip(30);

        // Display BitSet 
        System.out.println("bs.flip(30): " + bs);

        // By using flip(int st_bit , int en_bit) method 
        // is to flip the bit starting at st_bit and ending at 
        // before en_bit from bitset
        bs.flip(20, 40);

        // Display BitSet 
        System.out.println("bs.flip(20,40): " + bs);
    }
}

Output

输出量

bs: {10, 20, 30, 40, 50}
bs.flip(30): {10, 20, 40, 50}
bs.flip(20,40): {10, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 50}


翻译自: https://www.includehelp.com/java/bitset-flip-method-with-example.aspx

java bitset

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值