Skip to content

Instantly share code, notes, and snippets.

@deckarep
Created November 9, 2023 04:57
Show Gist options
  • Save deckarep/01fb21a13fd804d0c5fd0296240bf384 to your computer and use it in GitHub Desktop.
Save deckarep/01fb21a13fd804d0c5fd0296240bf384 to your computer and use it in GitHub Desktop.
A stupid simple Python utility to quickly print out the alignment of an integer provided on the command line.
import sys
def check_alignment(val):
alignments = {
'2-byte': val & 0x1 == 0,
'4-byte': val & 0x3 == 0,
'8-byte': val & 0x7 == 0,
'16-byte': val & 0xF == 0,
'32-byte': val & 0x1F == 0,
'64-byte': val & 0x3F == 0,
'128-byte': val & 0x7F == 0,
'256-byte': val & 0xFF == 0,
}
# Check all alignments and store those that are true
satisfied_alignments = [alignment for alignment, is_aligned in alignments.items() if is_aligned]
return satisfied_alignments
def number_aliases(numStr):
val = int(numStr, 16)
return {
'binary:': bin(val),
'decimal:': val,
'hex:': numStr,
}
# Example usage:
if len(sys.argv) > 1:
argument = sys.argv[1]
print("Integer aliases:")
for x in number_aliases(argument).items():
print(" ", x[0], str(x[1]))
print()
results = check_alignment(int(argument, 16))
if len(results) == 0:
print("This val: {} is only 1-byte aligned (NOT ALIGNED)".format(argument))
else:
print("The val: {} is aligned".format(argument))
for x in results:
print(" ", x)
else:
print("Provide a single integer value please.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment