Example Code

Reverse the Bits in an Integer in LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Download All

Overview

This example reverse an 8-bit integer using Rotate Left and Rotate Right functions

 

Description

It is useful to reverse the order of the bits that make up a integer.  An inefficient way of doing this would be to convert the integer to a Boolean array, reverse the Boolean array and then convert the Boolean array to an integer. However, this has the extra overhead of creating an array of Booleans, Which will really be represented in memory as an array of integers. To do this more efficiently we can use a couple of the Data Manipulation Functions which will allow us to operate on the bits of the integer itself.

The two Functions which we need are the Rotate Left With Carry and the Rotate Right With Carry.  These two functions rotate the bits in an integer in a given direction and output a Boolean value for the bit that was "rotated out." For example, if we take the binary representation of the number 5, 00000101, and rotate it right, we get 00000010 with a true carry out because the one was rotated out. We can use this by taking two integers, the one we want to rotate and a blank integer. We're going to rotate the bits out of the original integer and into the blank one, but do so in opposite directions.  This will end up reversing the bits in the integer.

 

As an example, we'll take a 4-bit representation of the number 5, 0101, and perform this operation on it.

Orig.    Carry   Blank

0101     -         0000     Start

0010     1        0001     the one is rotated out an into the right side of the blank

0001     0        0010

0000     1        0101

0000     0        1010

 

Requirements

LabVIEW Base Development System 2012 or compatible

 

Steps to Implement or Execute Code

  1. Enter the number to be reversed in the Input Numeric control
  2. Run the VI
  3. Notice the Output Numeric is the reversed value

Additional Information or References

I've added the two Convert to Boolean Arrays for demonstration purposes.  Those should be removed if this is used as a subVI

You can change the data type to be any integer by changing the representation and changing the input into N for the For Loop.

 

VI Block Diagram 

12176.PNG

**This document has been updated to meet the current required format for the NI Code Exchange. **

 

 

Jared S.
Applications Engineering
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.