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. Sublime Text 3 安装Go语言相关插件gosublime

    1.打开Sublime Text,使用快捷键 ctrl+` (左上角Tab键上方,Esc键下方)或者使用菜单 View > Show Console menu,此时将出现Sublime Text ...

  2. 用C#基于WCF创建TCP的Service供Client端调用

    本文将详细讲解用C#基于WCF创建TCP的Service供Client端调用的详细过程 1):首先创建一个Windows Service的工程 2):生成的代码工程结构如下所示 3):我们将Servi ...

  3. 移动端_line-height问题

    如果把line-height加1px,iPhone文字就会下移,由于我们app的ios用户居多,并且android机型太多,不同机型也会显示不同,所以只能退而求其次了. line-height的兼容问 ...

  4. T-SQL优化

    我们做软件开发的,大部分人都离不开跟数据库打交道,特别是erp开发的,跟数据库打交道更是频繁,存储过程动不动就是上千行,如果数据量大,人员流动大,那么我么还能保证下一段时间系统还能流畅的运行吗?我么还 ...

  5. 【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化

    接着来说这个JSON循环引用的问题: 关于JSON格式的转化,其实关键就是这几个依赖: <!-- json --> <!-- 1号 --> <dependency> ...

  6. 【JDBC 报错】Connections could not be acquired from the underlying database!

    项目启动报错: [2016-07-13 10:04:15,074] ERROR org.apache.ibatis.executor.BaseExecutor Could not get a data ...

  7. 利用pushState开发无刷页面切换

    转载:http://www.cnblogs.com/flash3d/archive/2013/10/23/3384823.html 实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与 ...

  8. css3 -- 网页字体

    1.@font-face规则 @font-face{ font-family:chunk; src:local('chunkFive'), url("chunkFive.ttf“) form ...

  9. ASP.NET Core EF Sample

    Install EF Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityF ...

  10. HDU5754 Life Winner Bo(博弈)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5754 Description Bo is a "Life Winner" ...