1

We can round off a number say 23 or 74 to 20 and 70 by seeing the numbers lsb(right most bit) and 26 or 78 to 30 and 80.. My doubt is if this is possible in verilog codes... I want to know after converting into digital will this concept be possible..

Satheesh.R
  • 47
  • 1
  • 4
  • Are you only Interested in rounding base 10 numbers? Or are you looking for a way to round binary numbers? – nguthrie Jan 22 '16 at 12:03
  • I wouldn't say LSB and MSB in this context, rather least significant digit and most significant digit. – Tudor Timi Jan 25 '16 at 08:52
  • oh yes sir.. I am facing another problem now. How shall i find the right most digit when am getting error for a = n1 % 10; as you know dividing by 10 and obtaining the remainder can easily give the right most digit of any number.. – Satheesh.R Jan 25 '16 at 13:49

2 Answers2

0

In general to round off integers of base n you add n/2 and the divide by n (discarding the remainder) and then multiply by n. So your examples above:

(23+5)/10 * 10 = 20
(28+5)/10 * 10 = 30

Doing this with binary logic is a bit expensive since you need to divide and multiply. However, if you are working with base 2 numbers then those operations are free since it is just a bit shift.

For an example in binary, let's say you want to round 61 to the nearest multiple of 8, which would be 64. In binary, 61 is 6'b011101. Add 8/2 (6'b000100) to this and you get 6'b100001 which is 65. Now divide by 8 and multiply by 8. Since that is just shift right by 3 and then shift left by three we can simply zero out the 3 lsbs to get 6'b100000 which is 64.

nguthrie
  • 2,615
  • 6
  • 26
  • 43
  • Thanks a lot for this.. can you provide me a divider verilog code as /10 is not synthesisable using xilinx.. – Satheesh.R Jan 29 '16 at 16:06
  • You'll have to figure that out on your own or find some IP. Xilinx may have a divider block that you can instantiate. When I need a divider I use one provided by Synopsys or Cadence with their synthesis tools. – nguthrie Jan 29 '16 at 16:08
  • You can pick an algorithm from the Internet and apply it: https://en.m.wikipedia.org/wiki/Division_algorithm – nguthrie Jan 31 '16 at 13:13
  • Thank you for your valuable comments. – Satheesh.R Feb 02 '16 at 05:24
0

In case you just want to make the LSB of any register zero you can always do something like this

reg [7:0] a;
reg [7:0] b = 23;

a = {b[7:1], 1'b0};
DBB
  • 467
  • 9
  • 23