[email protected] java bit自我practice##Q&A:为何int 来初始化size of bitVector?long,甚至是BigInteger等策略

/*
     * BitSets are packed into arrays of "words."  Currently a word is
     * a long, which consists of 64 bits, requiring 6 address bits.
     * The choice of word size is determined purely by performance concerns.
     */
    private final static int ADDRESS_BITS_PER_WORD = 6;
    private final static int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD;
    private final static int BIT_INDEX_MASK = BITS_PER_WORD - 1;

    /* Used to shift left or right for a partial word mask */
    private static final long WORD_MASK = 0xffffffffffffffffL;

/**
     * The internal field corresponding to the serialField "bits".
     */
    private long[] words;

    /**
     * The number of words in the logical size of this BitSet.
     */
    private transient int wordsInUse = 0;

/**
     * Given a bit index, return word index containing it.
     */
    private static int wordIndex(int bitIndex) {
        return bitIndex >> ADDRESS_BITS_PER_WORD;
    }

/**
     * Creates a new bit set. All bits are initially {@code false}.
     */
    public BitSet() {
        initWords(BITS_PER_WORD);
        sizeIsSticky = false;
    }

/**
     * Creates a bit set whose initial size is large enough to explicitly
     * represent bits with indices in the range {@code 0} through
     * {@code nbits-1}. All bits are initially {@code false}.
     *
     * @param  nbits the initial size of the bit set
     * @throws NegativeArraySizeException if the specified initial size
     *         is negative
     */
    public BitSet(int nbits) {
        // nbits can‘t be negative; size 0 is OK
        if (nbits < 0)
            throw new NegativeArraySizeException("nbits < 0: " + nbits);

        initWords(nbits);
        sizeIsSticky = true;
    }

    private void initWords(int nbits) {
        words = new long[wordIndex(nbits-1) + 1];
    }

   /**
     * Sets the bit at the specified index to {@code true}.
     *
     * @param  bitIndex a bit index
     * @throws IndexOutOfBoundsException if the specified index is negative
     * @since  JDK1.0
     */
    public void set(int bitIndex) {
        if (bitIndex < 0)
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);

        int wordIndex = wordIndex(bitIndex);
        expandTo(wordIndex);

        words[wordIndex] |= (1L << bitIndex); // Restores invariants

        checkInvariants();
    }

    /**
     * Sets the bit at the specified index to the specified value.
     *
     * @param  bitIndex a bit index
     * @param  value a boolean value to set
     * @throws IndexOutOfBoundsException if the specified index is negative
     * @since  1.4
     */
    public void set(int bitIndex, boolean value) {
        if (value)
            set(bitIndex);
        else
            clear(bitIndex);
    }

/**
     * Sets the bit specified by the index to {@code false}.
     *
     * @param  bitIndex the index of the bit to be cleared
     * @throws IndexOutOfBoundsException if the specified index is negative
     * @since  JDK1.0
     */
    public void clear(int bitIndex) {
        if (bitIndex < 0)
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);

        int wordIndex = wordIndex(bitIndex);
        if (wordIndex >= wordsInUse)
            return;

        words[wordIndex] &= ~(1L << bitIndex);

        recalculateWordsInUse();
        checkInvariants();
    }
时间: 2024-10-23 04:07:03

[email protected] java bit自我practice##Q&A:为何int 来初始化size of bitVector?long,甚至是BigInteger等策略的相关文章

hadoop错误Ignoring exception during close for [email&#160;protected] java.io.IOException Spill failed

1.错误    Ignoring exception during close for [email protected] java.io.IOException: Spill failed2.原因     本地磁盘空间不足非hdfs (我是在myeclipse中调试程序,本地tmp目录占满)3.解决     清理.增加空间 如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的[推荐]. 如果,您希望更容易地发现我的新博客,不妨点击一下左下角的[关注我]. 如果,您对我的博客所讲述的内容有

java.lang.NullPointerException Ignoring exception during close for [email&#160;protected]

1.问题描述:在调试mapreduce辅助排序(二次排序)的过程中,运行程序总是报错 Ignoring exception during close for [email protected]java.lang.NullPointerException at org.apache.hadoop.io.WritableComparator.compare(WritableComparator.java:157) at org.apache.hadoop.mapred.MapTask$MapOutp

[email&#160;protected]动态代理-类加载器

一.测试单元     概述:用于测试JAVA代码的工具类,已内置在Eclipse中;     格式:         1.在方法的上面添加@Test;         2.对被测试的方法的要求:权限-public;返回值-void;参数-空参         [email protected]:在@Test标注的方法前执行,可以用于初始化;           @After:在@Test标注的方法后执行,可以用于释放资源; 二.注解     概述:java的一种数据类型,和类/接口在同一级别  

hadoop错误[email&#160;protected]

错误: [email protected] java.io.IOException: Spill failed 错误原因: 本地磁盘空间不足非hdfs (我是在myeclipse中调试程序,本地tmp目录占满) 解决办法:     清理.增加空间. 如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的[推荐]. 如果,您希望更容易地发现我的新博客,不妨点击一下左下角的[关注我]. 如果,您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是[刘超★ljc]. 本文版权归作者和博客园共

[email&#160;protected]:在PC屏幕上显示Android手机屏幕

这里介绍一款工具——[email protected],用来获取手机屏幕,显示在PC屏幕上.它集截图.录像等多种功能于一体. 安装 1.    下载地址:http://droid-at-screen.org/download.html,下载后是一个jar包,放到某个目录就可以. 2.    安装JDK6或以上版本 3.    安装Android SDK(从Android官方下载一个完整包解压即可) 4.    设置ANDROID_HOME环境变量指向AndroidSDK主目录(这步如果不做,则需

Struts2报错异常Method &quot;setUser&quot; failed for object [email&#160;protected]

在写类型转换的时候发现报错 异常信息: 1 ognl.MethodFailedException: Method "setUser" failed for object [email protected] [java.lang.NoSuchMethodException: com.mikey.action.ConverterAction.setUser([Ljava.lang.String;)] 2 at ognl.OgnlRuntime.callAppropriateMethod(O

页面报错误:HTTP Status 500 - Method &quot;execute&quot; failed for object [email&#160;protected]解决方法?

16:21:07,308 ERROR DefaultDispatcherErrorHandler:42 - Exception occurred during processing request: Method "execute" failed for object [email protected] ognl.MethodFailedException: Method "execute" failed for object [email protected] [

@hdu - [email&#160;protected] Counting Stars

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 给定一个 n 点 m 边的无向图(无重边自环),求有多少子图形如,包含 4 个点 {A, B, C, D} 与 6 条边 {AB, BC, CD, DA, AC}. 原题链接. @[email protected] 一个并不常用的黑科技:三元环计数. mark一下博客地址. 注意到题目

SQL Server-聚焦WHERE [email&#160;protected] OR @Param IS NULL有问题?

前言 上一篇我们讲完SQL动态查询,本节我们继续来讲解SQL动态查询中存在的问题. SQL动态查询条件筛选过滤 当我们创建存储过程调用存储过程时,若筛选条件有值则过滤,没有值则返回所行记录,类似如下查询: WHERE ([email protected] OR @col IS NULL) 这样查询会存在什么问题呢?性能会不会有问题呢,这个是我们本节需要深入探讨的问题. 接下来我们创建如下测试表并插入测试数据,如下: CREATE TABLE Test ( SomeCol1 INT NOT NUL