# Convert string to binary robotics

In most microcontrollers, each pin can be controlled independently. Many controllers also let you work with multiple pins at a time. When pins are combined, they form a port. This makes the programming code easier for certain tasks, like operating a string of LEDs or a bunch of motors. Not all microcontroller ports have 8 bits.

Some are limited to just a few bits. Common in controllers are a full 8-bit-wide port and a second port with fewer bits. Regardless of the number of bits, the idea behind bitwise programming is the same.

This value corresponds to the bits you want to control. For example, given an 8-bit port, the number 54 in binary is Each programming language provides a different mechanism for these procedures, and what follows in the next few sections are simple approaches using Visual Basic.

I picked this language because many of you may already be at least somewhat familiar with it, even though you may not use it to program your robots. Other languages, such as C, offer more robust bit-handling operators that you can take advantage of.

The sample convert string to binary robotics that follows is meant more to teach you the fundamentals than to be applied directly convert string to binary robotics a robot.

Take the ideas and adapt them to your particular case. When combined as a group, binary 0s and 1s can represent any number in existence, though for robots, at least, numbers on the small end of the scale are most common. Bit Weight indicates how the position of the bits influences the resulting numeric value. You can count the bit weights yourself to arrive at the decimal value. Whenever there is a 1, you add its bit weight to the total:.

There are a number of programming approaches for determining the individual bits of a binary-formatted value. For instance, you may want to determine which bits are 0 and which are 1 given any 4-bit value from 0 to You could create a lookup table that matches all 16 possibilities to their binary equivalents, but there are several other methods you may want to use instead, depending convert string to binary robotics the commands and statements provided in the programming language you are using.

Remember that the individual bits of a port are typically convert string to binary robotics to as a number starting at convert string to binary robotics, not 1. This means that in the binary valuebit 0 has a value of 0; bit 3 is 1.

The bits are numbered 0, 1, 2, and 3. This code is Visual Basic—compatible and places the bit value 0 or 1 into a four-element array, Bit 0 to Bit 3.

While this example shows only 4-bit binary strings being used, the same technique can be used with 8- or bit strings:. The working part of this code is the For loop. The Val statement converts the isolated string character into a numeric digit.

Numeric values can be readily converted into strings once made into a string of 0s and 1s. The bit weights for a value between 0 and 15 are 1, 2, 4, and 8. This tests to see if the third bit bit weight of 4 is 0 or 1 for the number 7. Expressed in binary form, 7 is So, for this example, Convert string to binary robotics would contain a nonzero result—4, in this case.

But suppose you want to test for bit weight The following is a quick demonstration routine, suitable for use in Visual Basic and convert string to binary robotics compatible programming environments. Bits are counted 0 to 3. Change X to different values; change the If expression to experiment with different bit weights:. You will have plenty of occasions to convert a set of binary digits into a decimal value. This can be done with simple addition and multiplication, as shown in the Visual Basic—compatible code that follows.

Here, values for 4 bits have been specified and named D0 through D3. The message box displays the numeric equivalent of the bits you specify. For example, the bits result in 9. It is not uncommon to manipulate the individual bits of a computer or microcontroller port. Quite often, however, it is not possible or practical to address each individual bit. Rather, you must control the bits in sets of 4 or 8. Imagine you have a 4-bit port on your computer or microcontroller.

You control the setting of each bit by using a value from 0 to 15, with 0 representing the bitsand 15 representing the bits You need a way to control each bit separately, without changing the state of the other bits. For instance, suppose CurrentValue is 7 and you want to turn on bit 3 bit weight 8 as well:. The following is Visual Basic—compatible code that demonstrates the use of Or masking to convert string to binary robotics on specific bits, without changing the others.

For practice, change X to another value—say, 4 binary The result **convert string to binary robotics** now 6, which represents binary Released under Creative Commons 3.