[myhdl-list] intbv with min -1, max 1 has 2 bits?
Brought to you by:
jandecaluwe
From: Günter D. <dan...@we...> - 2008-06-12 14:56:52
|
Hi, I tried to create a signed intbv with one bit in the form: >>>a = intbv(-1,-1,1) >>>print "a: %d, min: %d, max: %d, _nrbits: %d"%( a, a.min, a.max, a._nrbits) a: -1, min: -1, max: 1, _nrbits: 2 It turns out that intbv uses 2 bits for it. Looking in the intbv.__init__() function there is this code section starting at line 46: if max is not None and min is not None: _nrbits = maxfunc(len(bin(max-1)), len(bin(min))) if min >= 0: _nrbits = len(bin(max-1)) elif max <= 0: _nrbits = len(bin(min)) else: # make sure there is a leading zero bit in positive numbers _nrbits = maxfunc(len(bin(max-1))+1, len(bin(min))) What is interesting about this is that the first assignment to _nrbits right after the if clause is actually overwritten by any of the following assignments. So the reason for the -1,1 range to have 2 bits seems to lay in the comment about having a leading zero bit for positive numbers. I might be oblivious and the reason is obvious, but I don't see it at the moment. Anyone can explain why I need a leading zero bit and cannot create a 1 bit intbv? Cheers, Guenter |