Advertisement
linuxlizard

Padded Binary

Feb 12th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. # Debugging a problem in a Verilog simulation. Needed a convenient way to print values as binary while padded to the bus width.
  2. class PaddedBinary(object):
  3.     def __init__(self,numbits):
  4.         self.numbits = numbits
  5.  
  6.     def __call__(self,num):
  7.         s = bin(num)[2:]  # trim off the leading '0b'
  8.         s = ('0'*(self.numbits-len(s))) + s
  9.         return s
  10.  
  11. padbin68 = PaddedBinary(68)
  12. print(padbin68(0x4242))
  13.  
  14. padbin48 = PaddedBinary(48)
  15. print(padbin48(0x4242))
  16.  
  17. output:
  18. 00000000000000000000000000000000000000000000000000000100001001000010
  19. 000000000000000000000000000000000100001001000010
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement