bitmap icon indicating copy to clipboard operation
bitmap copied to clipboard

Bitmap.Or(shortBitmap, longBitmap) result length same as the first one

Open slawdan opened this issue 3 years ago • 0 comments

When using multiple bitmaps as Bitmap.Or() parameters, the length of destination Bitmap will be same to the first bitmap, if first one is shorter than others, the result will be truncate to start bytes of the result.

Here is sample code to reproduce, see only in last case, call Or() one by one, get the right result.

package main

import (
	"fmt"
	"github.com/kelindar/bitmap"
)

func main() {
	bm := new(bitmap.Bitmap)
	bm1 := bitmap.FromBytes([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01})
	bm2 := bitmap.FromBytes([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01})
	bm3 := bitmap.FromBytes([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01})
	bm.Or(bm1, bm2, bm3)
	fmt.Println("Excepted:3, got:", bm.Count())
	bm.Clear()
	bm.Or(bm1, bm3, bm2)
	fmt.Println("Excepted:3, got:", bm.Count())
	bm.Clear()
	bm.Or(bm3, bm2, bm1)
	fmt.Println("Excepted:3, got:", bm.Count())
	fmt.Printf("bm:%+v\n", bm.ToBytes())
	bm.Clear()
	bm.Or(bm3, bm1, bm2)
	fmt.Println("Excepted:3, got:", bm.Count())
	fmt.Printf("bm:%+v\n", bm.ToBytes())
	bm.Clear()
	bm.Or(bm2, bm3, bm1)
	fmt.Println("Excepted:3, got:", bm.Count())
	bm.Clear()
	bm.Or(bm2, bm1, bm3)
	fmt.Println("Excepted:3, got:", bm.Count())
	bm.Clear()
	bm.Or(bm3)
	bm.Or(bm2)
	bm.Or(bm1)
	fmt.Println("Excepted:3, got:", bm.Count())
}

outputs,

Excepted:3, got: 1
Excepted:3, got: 1
Excepted:3, got: 32
bm:[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 69 120 99 101 112 116 101 101]
Excepted:3, got: 32
bm:[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 69 120 99 101 112 116 101 101]
Excepted:3, got: 2
Excepted:3, got: 2
Excepted:3, got: 3

Affected lib versions: v1.2.1, v1.4.1 (I found this issue on v1.2.1 and reproduced on v1.4.1) Affected Go versions: go1.17.6 darwin/amd64, go1.18.5 darwin/amd64 Affected OS: macOS Monterey 12.5.1

slawdan avatar Sep 05 '22 07:09 slawdan