Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 36 characters in body
Source Link
Basic Block
  • 781
  • 10
  • 19

Say you have a 32 bit integer, why not just move A into the first 16 bit half and B into the other?

def vec_pack(vec): return vec[0] + vec[1] * 65536; def vec_unpack(number): return [number % 65536, int(number // 65536)];65536]; 

Other than this being as space efficient as possible and cheap to compute, a really cool side effect is that you can do vector math on the packed number.

a = vec_pack([2,4]) b = vec_pack([1,2]) print(vec_unpack(a+b)) # [3, 6] Vector addition print(vec_unpack(a-b)) # [1, 2] Vector subtraction print(vec_unpack(a*2)) # [4, 8] VectorScalar multiplication 

that goes through these two points

Say you have a 32 bit integer, why not just move A into the first 16 bit half and B into the other?

def vec_pack(vec): return vec[0] + vec[1] * 65536; def vec_unpack(number): return [number % 65536, int(number / 65536)]; 

Other than this being as space efficient as possible and cheap to compute, a really cool side effect is that you can do vector math on the packed number.

a = vec_pack([2,4]) b = vec_pack([1,2]) print(vec_unpack(a+b)) # [3, 6] Vector addition print(vec_unpack(a-b)) # [1, 2] Vector subtraction print(vec_unpack(a*2)) # [4, 8] Vector multiplication 

that goes through these two points

Say you have a 32 bit integer, why not just move A into the first 16 bit half and B into the other?

def vec_pack(vec): return vec[0] + vec[1] * 65536; def vec_unpack(number): return [number % 65536, number // 65536]; 

Other than this being as space efficient as possible and cheap to compute, a really cool side effect is that you can do vector math on the packed number.

a = vec_pack([2,4]) b = vec_pack([1,2]) print(vec_unpack(a+b)) # [3, 6] Vector addition print(vec_unpack(a-b)) # [1, 2] Vector subtraction print(vec_unpack(a*2)) # [4, 8] Scalar multiplication 
deleted 73 characters in body
Source Link
Basic Block
  • 781
  • 10
  • 19

Say you have a 32 bit integer, why not just move A into the first 16 bit half and B into the other?

def vec_pack(vec): return vec[0] + vec[1] * 65536; def vec_unpack(number): return [number % 65536, int(number / 65536)]; 

Other than this being as space efficient as possible and cheap to compute, a really cool side effect is that you can do vector math on the packed number.

a = vec_pack([2,4]) b = vec_pack([1,2]) print(vec_unpack(a+b)) # [3, 6] Vector addition print(vec_unpack(a-b)) # [1, 2] Vector subtraction print(vec_unpack(a*2)) # [4, 8] Vector multiplication print(vec_unpack(a/b)) # [2, 0] First digit is the slope of the line that goes through these two points 

that goes through these two points

Say you have a 32 bit integer, why not just move A into the first 16 bit half and B into the other?

def vec_pack(vec): return vec[0] + vec[1] * 65536; def vec_unpack(number): return [number % 65536, int(number / 65536)]; 

Other than this being as space efficient as possible and cheap to compute, a really cool side effect is that you can do vector math on the packed number.

a = vec_pack([2,4]) b = vec_pack([1,2]) print(vec_unpack(a+b)) # [3, 6] Vector addition print(vec_unpack(a-b)) # [1, 2] Vector subtraction print(vec_unpack(a*2)) # [4, 8] Vector multiplication print(vec_unpack(a/b)) # [2, 0] First digit is the slope of the line that goes through these two points 

Say you have a 32 bit integer, why not just move A into the first 16 bit half and B into the other?

def vec_pack(vec): return vec[0] + vec[1] * 65536; def vec_unpack(number): return [number % 65536, int(number / 65536)]; 

Other than this being as space efficient as possible and cheap to compute, a really cool side effect is that you can do vector math on the packed number.

a = vec_pack([2,4]) b = vec_pack([1,2]) print(vec_unpack(a+b)) # [3, 6] Vector addition print(vec_unpack(a-b)) # [1, 2] Vector subtraction print(vec_unpack(a*2)) # [4, 8] Vector multiplication 

that goes through these two points

Source Link
Basic Block
  • 781
  • 10
  • 19

Say you have a 32 bit integer, why not just move A into the first 16 bit half and B into the other?

def vec_pack(vec): return vec[0] + vec[1] * 65536; def vec_unpack(number): return [number % 65536, int(number / 65536)]; 

Other than this being as space efficient as possible and cheap to compute, a really cool side effect is that you can do vector math on the packed number.

a = vec_pack([2,4]) b = vec_pack([1,2]) print(vec_unpack(a+b)) # [3, 6] Vector addition print(vec_unpack(a-b)) # [1, 2] Vector subtraction print(vec_unpack(a*2)) # [4, 8] Vector multiplication print(vec_unpack(a/b)) # [2, 0] First digit is the slope of the line that goes through these two points