Skip to content

Instantly share code, notes, and snippets.

@yoshikakbudto
Created March 18, 2021 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshikakbudto/0f8a3347a900cdbcfb5509a0736c481d to your computer and use it in GitHub Desktop.
Save yoshikakbudto/0f8a3347a900cdbcfb5509a0736c481d to your computer and use it in GitHub Desktop.
convert version numbers into one number
#!/usr/bin/env python
"""
Convert version number of only digits into one number, ready for comparing.
"""
v1=(1,20,0,10)
v2=(1,20,1,0)
# length in bits of each version number.
# for example '8' allows to use version number with elements of a max 256: "256.256.256.256"
VER_ELEM_BITS=8
def ver_to_num(a,b,c,d):
res = (a<<(VER_ELEM_BITS*3)) + (b<<(VER_ELEM_BITS*2)) + (c<<VER_ELEM_BITS) + d
return res
v1=ver_to_num(*v1)
v2=ver_to_num(*v2)
if v2 > v1:
print('ok',v2)
elif v2 == v1:
print('equal',v2)
else:
print('v2 should be greater',v2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment