Truncating bits using bitwise logic

Document created by BrettG on Feb 18, 2013
Version 1Show Document
  • View in full screen mode

A customer recently asked me if there was an easy way to truncate the bottom LSBs of the data in a SigmaDSP so it could easily be output via the serial port to a DSP or DAC with a lower bit depth. In particular, the goal was to get the 28-bit data from the SigmaDSP core and output it on the serial port as 16-bit data. Luckily, on the 3rd generation SigmaDSP cores, it is easy to do this using the bitwise logic operators in the Basic DSP - Bitwise library in the Toolbox.


ScreenHunter_19 Feb. 18 17.23.jpg


I use a test input that makes use of all of all bits of precision in the core. I then route that to the bitwise AND function and and it with a -1 in 20.8 format, which gives you 0b1111111111111111111100000000... in other words, the bottom 8 bits are zero, and the remaining bits are ones. This lets the MSBs pass through while the LSBs are set to zero.


ScreenHunter_19 Feb. 18 17.24.jpg


You can see that my test input is 0x9E147B, and the result of the operation is 0x9E1400. So, this shows that the bottom 8 bits are effectively set to zero.


This, of course, works with negative numbers as well.

ScreenHunter_19 Feb. 18 17.27.jpg