gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

pack.go (2651B)


      1 package bitutil
      2 
      3 // PackInt8s will pack two signed 8bit integers into an unsigned 16bit integer.
      4 func PackInt8s(i1, i2 int8) uint16 {
      5 	const bits = 8
      6 	const mask = (1 << bits) - 1
      7 	return uint16(i1)<<bits | uint16(i2)&mask
      8 }
      9 
     10 // UnpackInt8s will unpack two signed 8bit integers from an unsigned 16bit integer.
     11 func UnpackInt8s(i uint16) (int8, int8) {
     12 	const bits = 8
     13 	const mask = (1 << bits) - 1
     14 	return int8(i >> bits), int8(i & mask)
     15 }
     16 
     17 // PackInt16s will pack two signed 16bit integers into an unsigned 32bit integer.
     18 func PackInt16s(i1, i2 int16) uint32 {
     19 	const bits = 16
     20 	const mask = (1 << bits) - 1
     21 	return uint32(i1)<<bits | uint32(i2)&mask
     22 }
     23 
     24 // UnpackInt16s will unpack two signed 16bit integers from an unsigned 32bit integer.
     25 func UnpackInt16s(i uint32) (int16, int16) {
     26 	const bits = 16
     27 	const mask = (1 << bits) - 1
     28 	return int16(i >> bits), int16(i & mask)
     29 }
     30 
     31 // PackInt32s will pack two signed 32bit integers into an unsigned 64bit integer.
     32 func PackInt32s(i1, i2 int32) uint64 {
     33 	const bits = 32
     34 	const mask = (1 << bits) - 1
     35 	return uint64(i1)<<bits | uint64(i2)&mask
     36 }
     37 
     38 // UnpackInt32s will unpack two signed 32bit integers from an unsigned 64bit integer.
     39 func UnpackInt32s(i uint64) (int32, int32) {
     40 	const bits = 32
     41 	const mask = (1 << bits) - 1
     42 	return int32(i >> bits), int32(i & mask)
     43 }
     44 
     45 // PackUint8s will pack two unsigned 8bit integers into an unsigned 16bit integer.
     46 func PackUint8s(u1, u2 uint8) uint16 {
     47 	const bits = 8
     48 	const mask = (1 << bits) - 1
     49 	return uint16(u1)<<bits | uint16(u2)&mask
     50 }
     51 
     52 // UnpackUint8s will unpack two unsigned 8bit integers from an unsigned 16bit integer.
     53 func UnpackUint8s(u uint16) (uint8, uint8) {
     54 	const bits = 8
     55 	const mask = (1 << bits) - 1
     56 	return uint8(u >> bits), uint8(u & mask)
     57 }
     58 
     59 // PackUint16s will pack two unsigned 16bit integers into an unsigned 32bit integer.
     60 func PackUint16s(u1, u2 uint16) uint32 {
     61 	const bits = 16
     62 	const mask = (1 << bits) - 1
     63 	return uint32(u1)<<bits | uint32(u2)&mask
     64 }
     65 
     66 // UnpackUint16s will unpack two unsigned 16bit integers from an unsigned 32bit integer.
     67 func UnpackUint16s(u uint32) (uint16, uint16) {
     68 	const bits = 16
     69 	const mask = (1 << bits) - 1
     70 	return uint16(u >> bits), uint16(u & mask)
     71 }
     72 
     73 // PackUint32s will pack two unsigned 32bit integers into an unsigned 64bit integer.
     74 func PackUint32s(u1, u2 uint32) uint64 {
     75 	const bits = 32
     76 	const mask = (1 << bits) - 1
     77 	return uint64(u1)<<bits | uint64(u2)&mask
     78 }
     79 
     80 // UnpackUint32s will unpack two unsigned 32bit integers from an unsigned 64bit integer.
     81 func UnpackUint32s(u uint64) (uint32, uint32) {
     82 	const bits = 32
     83 	const mask = (1 << bits) - 1
     84 	return uint32(u >> bits), uint32(u & mask)
     85 }