美文网首页
Bitmap——位图

Bitmap——位图

作者: 昔日的帅哥 | 来源:发表于2019-08-15 10:19 被阅读0次

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zw on 2019/4/16.
 * 使用位图存储1000万个正整数,节省内存
 */
public class Bitmap {

    //1000万个正整数
    private static final int N = 10000000;
    
    //使用int数组进行存储,每个元素代表32个数字
    private int num[] = new int[N / 32 + 1];

    public void addValue(int value) {
        //int i = value % 32
        int i = value & 0x1f;

        //int index = value / 32;
        int index = value >> 5;
        
        //将1左移i位,做与运算
        num[index] |= (1 << i);
    }

    public void display(int row) {
        System.out.println("BitMap位图展示");
        for (int i = 0; i < row; i++) {
            List<Integer> list = new ArrayList<>();
            int temp = num[i];
            for (int j = 0; j < 32; j++) {
                list.add(temp & 1);
                temp >>= 1;
            }
            System.out.println("a[" + i + "]" + list);
        }
    }

    // 判断所在的bit为是否为1
    public boolean exists(int value) {
        int index = value >> 5;
        int i = value & 0x1f;
        return (num[index] & (1 << i)) != 0;
    }

    public static void main(String[] args) {
        int num[] = {1231231, 5, 30, 32, 64, 56, 159, 120, 21, 17, 35, 45};
        Bitmap map = new Bitmap();
        for (int i : num) {
            map.addValue(i);
        }

        int temp = 5;
        if(map.exists(temp)){
            System.out.println("temp:" + temp + "has already exists");
        }
        map.display(5);
    }
}

main result:

temp:5has already exists
BitMap位图展示
a[0][0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
a[1][1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
a[2][1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
a[3][0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
a[4][0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

Bitmap有两个比较局限的地方:

  1. 当样本分布极度不均匀的时候,Bitmap会造成很大空间上的浪费
    比如有10个数,分别是1、2、3、4、5、6、7、8、99999999999;那么你不得不用99999999999个bit位去实现你的Bitmap,而这个Bitmap的中间绝大多数位置都是0,并且永远不会用到。
  2. 当元素不是整型的时候,Bitmap就不适用了

相关文章

  • 图像编程学习历程-20161212-bmp文件的结构

    bmp 文件的组成结构 位图文件头 (bitmap-file header)位图信息头 (bitmap-infor...

  • Android中图片的压缩方法

    Bitmap Android中图片是以Bitmap(位图)形式存在,位图常见的文件格式有:.bmp .jpg .p...

  • Android 动画

    一、Drawable目录(res/drawable) 1、bitmap ,位图

  • Bitmap与Drawable

    Bitmap和Drawable分别是什么 bitmap:****位图(Bitmap),又称栅格图(英语:Raste...

  • Bitmap

    一、Bitmap简介 Bitmap是位图文件,扩展名可以是.bmp或者.dib。 位图是Windows标准格式图形...

  • Quartz 2D 编程指南十一:位图和图像遮罩(1)

    Bitmap Images and Image Masks 位图和图片遮照 Bitmap images and i...

  • Canvas使用技巧

    1、绘制几何图像/位图 void drawBitmap(Bitmap bitmap, float left, fl...

  • 位图-BitMap

    BitMap 字面意思解释为位图,准确翻译为基于位的映射 What is 基于位的映射? 就是用一个bit位来标记...

  • Bitmap——位图

    Bitmap有两个比较局限的地方: 当样本分布极度不均匀的时候,Bitmap会造成很大空间上的浪费比如有10个数,...

  • 位图BitMap

    位 1byte = 8bit内部只有0和1每一位用来存储某种状态,适合大数据,但是数据状态并不多的情况。 优缺点 ...

网友评论

      本文标题:Bitmap——位图

      本文链接:https://www.haomeiwen.com/subject/jlpajctx.html