In this lesson we explore how to create a Binary or Hexadecimal Bit Flipper. From our earlier lessons you see we can visually represent Hexadecimal or Binary numbers with a series of LED, with an on LED representing a “1” and an off LED representing a “0”. In programming and circuit applications, it is sometimes useful to “flip” or invert the bits. For an 8 bit number, one could do this in a program with 255 IF statements, but there is a simpler way. If you think about it, you can get the flippedByte by simple subtracting the byte from 0xFF, or 0b11111111. If you try some test cases, you can see that this will always work.

Simply stated, flippedByte=0xFF-Byte,

or if you prefer thinking in binary,

flippedByte=0b11111111-Byte

This is the circuit we are using to drive the 8 LED with the 74HX595 chip, and all this was explained in Tutorial 42.

This is the code which we developed in the video above.

The purpose of this lesson is to assign you homework. Your homework is to create a Bit Flipper. That is, for an 8 bit Binary or Hex number, invert the bits . . . “1” bits should become “0” and “0” bits should become “1”. For example,

if myByte=00001111

the flipped version of this would be

myByteFlipped=11110000

Similarly if myByte=00000001

myByteFlipped=11111110

You could do this with 255 if statements, but see if you can figure out a better way of doing it, and then demonstrate your results using the circuit we have been using in the last few lessons.

In this lesson we explore the Circular Shift Left (CSL) and Circular Shift Right (CSR) binary functions. These functions are similar to the Logical Shift Left and Logical Shift Right functions explained in the previous lesson.

As a reminder, we are doing this work on the circuit built in Tutorial 42. As a refresher, we are using the 74HC595 Serial to Parallel converter, connected to an Arduino according to the following schematic.

In all these lessons we are using the Arduino Super Starter Kit, which you can pick up HERE.

The Circular Shift Left function is illustrated below:

You can see that all bits shift to the left, with the Most Significant Bit, or MSB looping back to the Least Significant Bit, or LSB position.

Similarly, the Circular Shift Right Function is as follows:

There is actually a very easy way to implement this in a program. We illustrate it with an arduino program. Lets first consider the CSL function. We will first consider how to move the MSB to the LSB position. Remember from our earlier lessons, that dividing a binary number by 2 moves the bits to the right. We want to move the MSB 7 positions to the right, so we would divide by 2^{7}, which is 128. So, if we have a binary number myByte, we could move the MSB to the LSB by the equation:

myByte=myByte/128

With this we do the hardest part, which is to get the MSB to the LSB. Now we need to get the rest of the bits back, and in the left shifted position, we do that by multiplying by two. By combining these two functions, we end up with the solution of CSL being:

myByte=myByte/128+myByte*2

This simple equation will perform the CSL operation, when myByte is a Hexadecimal or Binary number (Byte type in Arduino).

We show the code below to implement the CSL an arduino with a 74HC595 chip:

Arduino

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

intlatchPin=11;

intclockPin=9;

intdataPin=12;

intdt=1000;

bytemyByte=0b11111110;

voidsetup(){

// put your setup code here, to run once:

Serial.begin(9600);

pinMode(latchPin,OUTPUT);

pinMode(dataPin,OUTPUT);

pinMode(clockPin,OUTPUT);

}

voidloop(){

// put your main code here, to run repeatedly:

digitalWrite(latchPin,LOW);

shiftOut(dataPin,clockPin,LSBFIRST,myByte);

digitalWrite(latchPin,HIGH);

Serial.println(myByte,BIN);

delay(dt);

myByte=myByte/128+myByte*2;

}

Similarly, the CSR binary function can be achieved with the equation:

In this lesson, we explore how to perform Logical Shift Left (LSL), and Logical Shift Right (LSR) functions on binary numbers, and we implement a circuit to perform these functions using an Arduino and a 74HC595 chip. We will demonstrate these functions on 8 bit binary numbers.

We start with the basic circuit and code which were developed in Lesson 42. In this lesson we are using parts from the Elegoo Arduino kit, which you can get HERE. We start with this circuit, which was explained in Lesson 42.

You can see that with this circuit, an 8 bit binary number can be visually displayed by illuminating the circuit LED. The goal of this lesson is to write code to perform LSL and LSR functions. The graphics below show conceptually how simple these functions are:

MSB stands for “Most Significant Bit” and LSB stands for “Least Significant Bit”. You can see that the LSL function just moves each bit one to the left, and fills the empty LSB with a “0”.

The LSR funtion is just as simple as illustrated below.

Such shifts are often required when doing digital logic, so it is important to understand what the terms mean.

We can see that the LSL function can be performed by simply multiplying the binary number by 2. Similarly the LSR function can be performed by dividing the binary number by 2.

Code for LSL:

Arduino

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

intlatchPin=11;

intclockPin=9;

intdataPin=12;

intdt=1000;

bytemyByte=0b10000000;//Put your binary number here

In lesson 42 we showed you how to connect and program the 74HC595 shift register. We showed how data in byte format would then be written to an array of 8 LED to give a visual representation of the binary version of that byte variable. We then gave you the assignment to create a Binary Counter using the 4HC595. In this lesson we show you the solution. This builds on Lesson 42, so make sure to have your basic 74HC595 circuit set up before starting this lesson.

In this lesson we are using parts from the Elegoo Arduino kit, which you can get HERE.

The code we ended up developing in this lesson is provided below.

Arduino

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

intlatchPin=11;

intclockPin=9;

intdataPin=12;

intdt=1000;

byteLED1s=0b00000000;

voidsetup(){

// put your setup code here, to run once:

Serial.begin(9600);

pinMode(latchPin,OUTPUT);

pinMode(dataPin,OUTPUT);

pinMode(clockPin,OUTPUT);

}

voidloop(){

// put your main code here, to run repeatedly:

digitalWrite(latchPin,LOW);

shiftOut(dataPin,clockPin,LSBFIRST,LED1s);

digitalWrite(latchPin,HIGH);

Serial.println(LED1s,BIN);

delay(dt);

LED1s=LED1s+1;

}

Making The World a Better Place One High Tech Project at a Time. Enjoy!