package bitmap;

public class BitMap {
private byte[] bytes; public BitMap(byte[] bytes) {
super();
this.bytes = bytes;
} public BitMap() {
super();
} public BitMap(int size) {
super();
int number = size / 8 + 1;// may waste a byte, which does not matter
bytes = new byte[number];
} /**
*
* @param n
* n>=1
*/
public void setBit(int n) {
if (n <= 0)
return;
int index = -1;
int offset = -1;
if (0 == n % 8) {
index = n / 8 - 1;
offset = 7;
} else {
index = n / 8;
offset = n % 8 - 1;
}
switch (offset) {
case 0:
bytes[index] = (byte)(bytes[index]|0x01);
break;
case 1:
bytes[index] = (byte)(bytes[index]|0x02);
break;
case 2:
bytes[index] = (byte)(bytes[index]|0x04);
break;
case 3:
bytes[index] = (byte)(bytes[index]|0x08);
break;
case 4:
bytes[index] = (byte)(bytes[index]|0x10);
break;
case 5:
bytes[index] = (byte)(bytes[index]|0x20);
break;
case 6:
bytes[index] = (byte)(bytes[index]|0x40);
break;
case 7:
bytes[index] = (byte)(bytes[index]|0x80);
break;
}
} public boolean get(int n){
if (n <= 0)
return false;
int index = -1;
int offset = -1;
if (0 == n % 8) {
index = n / 8 - 1;
offset = 7;
} else {
index = n / 8;
offset = n % 8 - 1;
}
switch (offset) {
case 0:
return (byte)(bytes[index]&0x01)!=0;//2^0
case 1:
return (byte)(bytes[index]&0x02)!=0;
case 2:
return (byte)(bytes[index]&0x04)!=0;
case 3:
return (byte)(bytes[index]&0x08)!=0;
case 4:
return (byte)(bytes[index]&0x10)!=0;
case 5:
return (byte)(bytes[index]&0x20)!=0;
case 6:
return (byte)(bytes[index]&0x40)!=0;
case 7:
return (byte)(bytes[index]&0x80)!=0;
}
return false;
} public static void main(String[] args) {
BitMap bMap = new BitMap(60);
bMap.setBit(59);
bMap.setBit(20);
bMap.setBit(21);
for(int i=1;i<=60;i++){
if(bMap.get(i)==true)
System.out.println(i);
}
} }

java实现BitMap的更多相关文章

  1. Java的bitmap到C

    在很多情况下android程序员需用到c,bimap这个是一个java中的类,android底层有一个skbitmap类和其对应.先在我遇到了需要把java的bitmap传递到底层中进行一些操作.现在 ...

  2. java使用bitmap求两个数组的交集

    一般来说int代表一个数字,但是如果利用每一个位 ,则可以表示32个数字 ,在数据量极大的情况下可以显著的减轻内存的负担.我们就以int为例构造一个bitmap,并使用其来解决一个简单的问题:求两个数 ...

  3. 【算法与数据结构专场】BitMap算法基本操作代码实现

    上篇我们讲了BitMap是如何对数据进行存储的,没看过的可以看一下[算法与数据结构专场]BitMap算法介绍 这篇我们来讲一下BitMap这个数据结构的代码实现. 回顾下数据的存储原理 一个二进制位对 ...

  4. Android笔记--Bitmap

    Android | Bitmap解析 Android中Bitmap是对图像的一种抽象.通过他可以对相应的图像进行剪裁,旋转,压缩,缩放等操作.这里循序渐进的一步步了解Bitmap的相关内容. 先了解B ...

  5. Android图像处理之BitMap(2)

    Bitmap 相关 1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: * png图片 如:R.drawable.tianjin J ...

  6. Android Bitmap变迁与原理解析(4.x-8.x)

    App开发不可避免的要和图片打交道,由于其占用内存非常大,管理不当很容易导致内存不足,最后OOM,图片的背后其实是Bitmap,它是Android中最能吃内存的对象之一,也是很多OOM的元凶,不过,在 ...

  7. 图片系列(6)不同版本上 Bitmap 内存分配与回收原理对比

    请点赞关注,你的支持对我意义重大. Hi,我是小彭.本文已收录到 GitHub · AndroidFamily 中.这里有 Android 进阶成长知识体系,有志同道合的朋友,关注公众号 [彭旭锐] ...

  8. Android之三种网络请求解析数据(最佳案例)

    AsyncTask解析数据 AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法. ...

  9. Android 保存图片到SQLite

    [转:原文] Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.ico ...

随机推荐

  1. [Unity3D插件]2dtoolkit系列二 动画精灵的创建以及背景图的无限滚动

    经过昨天2dtoolkit系列教程一的推出,感觉对新手还有有一定的启发作用,引导学习使用unity 2dToolKit插件的使用过程,今天继续系列二——动画精灵的创建,以及背景图的无限循环滚动,在群里 ...

  2. POJ 1987 Distance Statistics 树分治

    Distance Statistics     Description Frustrated at the number of distance queries required to find a ...

  3. 为什么我们可以使用while(~scanf("%d"))读到文件末尾

    经过测试文件末尾是一个标志位EOF 在c语言里我们用int来输出EOF 可以发现EOF等于-1 我们之前那个文章已经写过了..在c语言里负数的存储策略是补码 [-1]的补码=~(1)+1 那么就是比如 ...

  4. loadrunner统计字符串中指定字符出现的次数

    Action() { char *str="sdfas1,sdfsdf2,sdfsdfsdfdsf3,sdfsdfsdfsdfds4,fsdfdsf5,sdfdsfsd6,fsdfsd7sd ...

  5. Spring事务解析3-增强方法的获取

    从InfrastructureAdvisorAutoProxyCreator的层次结构中可以看到,InfrastructureAdvisorAutoProxyCreator间接实现了SmartInst ...

  6. 5、利用控件TVCLZip和TIdFTP压缩文件并上传到FTP的线程单元pas 改进版

    用到临界区 保护写日志的函数: 递归函数 删除目录下的所有文件: 循环创建或判断FTP的目录: 可改进的地方:循环压缩深层次目录的所以文件: 实现断点续传,或断点下载: {************** ...

  7. 记一次Runtime的巧用

    背景 我们的视频直播是用的大华乐橙的解决方案,而他们近期出来个新的SDK,并且对老版SDK不兼容,而这周,终于把大华乐橙的新版SDK切换了,和这一周做的新的东西,一起提交审核了,并且今天也通过审核了. ...

  8. Understanding the WPF Layout System

    Many people don't understand how the WPF layout system works, or how that knowledge can help them in ...

  9. 安装SQL2008 提示 创建usersettings/microsoft.sqlserver.configuration.landingpage.properties.se

    System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServer.Configuration ...

  10. UIImage两种初始化的区别

    UIImage可以通过以下两种方式进行初始化: //第一种初始化方式:[注意使用这种初始化的时候如果是png格式的可以不给后缀名,根据屏幕的的分辨率去匹配图片] UIImage *image = [U ...