summaryrefslogtreecommitdiffstats
path: root/lldb/examples/customization/bin-utils/binutils.py
diff options
context:
space:
mode:
authorJohnny Chen <johnny.chen@apple.com>2011-10-28 01:09:53 +0000
committerJohnny Chen <johnny.chen@apple.com>2011-10-28 01:09:53 +0000
commit2a781097f37bab041ed2cafb7cbcd95096fad46c (patch)
tree4c19ceaa6597abc009109402d024b1ae6e7a61ba /lldb/examples/customization/bin-utils/binutils.py
parent9a43e12117fdb6eb76b6f9e5863a6fe95a640b86 (diff)
downloadbcm5719-llvm-2a781097f37bab041ed2cafb7cbcd95096fad46c.tar.gz
bcm5719-llvm-2a781097f37bab041ed2cafb7cbcd95096fad46c.zip
Add a binutils.py which contains Python functions for displaying bit representation of numbers.
llvm-svn: 143173
Diffstat (limited to 'lldb/examples/customization/bin-utils/binutils.py')
-rw-r--r--lldb/examples/customization/bin-utils/binutils.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/lldb/examples/customization/bin-utils/binutils.py b/lldb/examples/customization/bin-utils/binutils.py
new file mode 100644
index 00000000000..09ef5152c22
--- /dev/null
+++ b/lldb/examples/customization/bin-utils/binutils.py
@@ -0,0 +1,53 @@
+"Collection of tools for displaying bit representation of numbers."""
+
+def binary(n, width=None):
+ """
+ Return a list of (0|1)'s for the binary representation of n where n >= 0.
+ If you specify a width, it must be > 0, otherwise it is ignored. The list
+ could be padded with 0 bits if width is specified.
+ """
+ l = []
+ if width and width <= 0:
+ width = None
+ while n > 0:
+ l.append(1 if n&1 else 0)
+ n = n >> 1
+
+ if width:
+ for i in range(width - len(l)):
+ l.append(0)
+
+ l.reverse()
+ return l
+
+def twos_complement(n, width):
+ """
+ Return a list of (0|1)'s for the binary representation of a width-bit two's
+ complement numeral system of an integer n which may be negative.
+ """
+ val = 2**(width-1)
+ if n >= 0:
+ if n > (val-1):
+ return None
+ # It is safe to represent n with width-bits.
+ return binary(n, width)
+
+ if n < 0:
+ if abs(n) > val:
+ return None
+ # It is safe to represent n (a negative int) with width-bits.
+ return binary(val*2 - abs(n))
+
+# print binary(0xABCD)
+# [1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1]
+# print binary(0x1F, 8)
+# [0, 0, 0, 1, 1, 1, 1, 1]
+# print twos_complement(-5, 4)
+# [1, 0, 1, 1]
+# print twos_complement(7, 4)
+# [0, 1, 1, 1]
+# print binary(7)
+# [1, 1, 1]
+# print twos_complement(-5, 64)
+# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1]
+
OpenPOWER on IntegriCloud