用一个int型保存32个boolean类型的值
来源:互联网 发布:北理工网络教育 编辑:程序博客网 时间:2024/06/03 04:20
#转自https://www.oschina.net/code/snippet_196697_36379#感谢~~~~~~~
/*这是以前遇到过的一道题目,最近在开源中国找到了比较简单的解决方法。*/
此类是从asp.net源代码中找到的一个很有用的类。现将此类翻译成java。
一个SimpleBitVector32类型的对象能保存32个标志位,相当于32个boolean类型的变量,但在SimpleBitVector32内部只是使用了一个int类型的字段,这样就用一个int类型的变量来替代32个boolean类型的变量,大大节省了内存开销。
/** * Created by Leon on 2017/5/21. */public class SimpleBitVector32{ private int data; public int getIntegerValue(){ return this.data; } public void setIntegerValue(int value){ this.data = value; } public boolean getThis(int bit){ return (this.data & bit) == bit; } public void setThis(int bit, boolean value){ int num = this.data; if (value) this.data = num | bit; else this.data = num & ~bit; } public int getThis(int mask, int offset){ return (this.data & mask) >> offset; } public void setThis(int mask, int offset, int value){ this.data = this.data & ~mask | value << offset; } public SimpleBitVector32(int data){ this.data = data; } public void set(int bit){ this.data |= bit; } public void clear(int bit){ this.data &= ~bit; }}
测试代码:
/** * Created by Leon on 2017/5/21. */public class Main { public static void main(String[] args) { SimpleBitVector32 data = new SimpleBitVector32(0); System.out.println("The old boolean value at index 0:" + data.getThis(1)); System.out.println("The old boolean value at index 1:" + data.getThis(2)); System.out.println("The old boolean value at index 2:" + data.getThis(2*2)); System.out.println("The old boolean value at index 3:" + data.getThis(2*2*2)); System.out.println("The old boolean value at index 3:" + data.getThis(2*2*2*2)); System.out.println("The old boolean value at index 5:" + data.getThis(2*2*2*2*2)); System.out.println(); data.setThis(1, true); data.setThis(2, false); data.setThis(2*2, true); data.setThis(2*2*2, false); data.setThis(2*2*2*2, true); data.setThis(2*2*2*2*2, false); System.out.println("The new boolean value at index 0:" + data.getThis(1)); System.out.println("The new boolean value at index 1:" + data.getThis(2)); System.out.println("The new boolean value at index 2:" + data.getThis(2*2)); System.out.println("The new boolean value at index 3:" + data.getThis(2*2*2)); System.out.println("The new boolean value at index 4:" + data.getThis(2*2*2*2)); System.out.println("The new boolean value at index 5:" + data.getThis(2*2*2*2*2)); System.out.println("The value inside SimpleBitVector32 is:" + data.getIntegerValue()); System.out.print("//"); for (int i = 31; i >= 0; i--) { System.out.print(((data.getIntegerValue() & (1 << i)) >>> i)); } }}
阅读全文
0 0
- 用一个int型保存32个boolean类型的值
- 数据库用一个int类型的字段保存前端多个字段(该字段为多选)
- int和boolean,String类型的数组存值输出问题
- SharedPreferences类供开发人员保存和获取基本数据类型的键值对.该类主要用于基本类型, 例如boolean,float.int,long和string. SharedPr
- Java 8 种数据类型 字节大小,int 4个字节,boolean几个字节
- JS中Boolean类型值的判断
- Javascript的Boolean类型
- Javascript的Boolean类型
- 写一个函数返回参数值为1的个数 比如: 15 00001111 4个1 程序类型: int count_one_bits(unsigned int value)
- java中 string和int boolean类型值true和String String类型值"true"和boolean类型 三种相互转换
- Boolean值判断2个条件之后选择一个条件
- 关于LayoutInflater.inflater(int resId,ViewGroup root,boolean attchtoroot)第三个参数的问题
- 用最优的算法求出一个int类型数组中的第二大值
- 一个int类型到底占多少个字节
- 一个int类型到底占多少个字节
- 一个int类型到底占多少个字节?
- android sharedpreference保存boolean,int,float,long,String和图片的使用方法及实例(附快速使用类文件)
- boolean 类型的条件赋值
- Java--文本编辑器
- TCP报头中URG、PSH标志详解
- 03.java注释
- USACO 1.2 Milking Cows
- Java线程的状态
- 用一个int型保存32个boolean类型的值
- 制作静态链接库并使用
- php -i 与 php -m
- java 设计模式 —— 浅析代理模式
- IE,谷歌浏览器的兼容性问题总结
- JavaScript前端常见算法面试题总结
- MySQL学习笔记-约束以及修改数据表
- CentOS下RPM方式安装MySQL5.6
- python魔法方法——(__new__)