March 2019

Friday, March 22, 2019

How to Subtract In Binary Using 2'S Complement


Description

Interested in subtracting in binary using 2's complement? In this final episode in the Bits of Binary series, Harley begins with what we know about subtracting in decimal and applies that to binary. Along the way way he introduces 2's complement and shows how it makes subtracting binary numbers a matter of simple addition.

Here at House of Hacks we do tutorials, project overviews, tool reviews and more related to making things around the home and shop. Generally this involves wood and metal working, electronics, photography and other similar things. If this sounds interesting to you, you may subscribe here.

If you’re interested in learning more about the House of Hacks' values, here’s a playlist for you.

And here’s the most recent video.

For a written transcript, go to How to Subtract In Binary Using 2'S Complement

Music under Creative Commons License By Attribution 3.0 by Kevin MacLeod at http://incompetech.com.
Intro/Exit: "Hot Swing"

Transcript

This is an elegant but non-intuitive way to represent negative numbers.

On this episode of Bits of Binary, I'll explain how it works, here at the House of Hacks.

Hi Makers, Builders and Do-It-Yourselfers. Harley here.

This is the last planned episode in the Bits of Binary series.

Previous episodes explored the concept of alternate number systems and how to do various mathematical operations in binary.

If you missed any of these episodes or want a refresher, they're all in this playlist.

In this past episode, I explained how to subtract a smaller number from a larger one.

It introduced subtraction and related it with how you know how to do it in decimal.

But by limiting the scope to smaller numbers subtracted from larger ones, I avoided the topic of how to deal with negative numbers.

Let's look at negative numbers and how computers represent them.

When learning decimal in grade school, we started with only positive numbers.

Once we got that under our belt, we were introduced to negative numbers, where the number line just mirrored the positive side with a negative sign in front of the number.

We could do the same thing with binary; just add a negative sign in front.

And if humans were the primary users of binary math, this would probably be sufficient, just like it is in decimal.

But we have to remember that the most common use of binary numbers is inside computers.

In the digital electronic environment of voltages, currents and electrons, we don't have the luxury of introducing another abstract symbol like a minus sign.

Ultimately, everything has to reduce to an on or off value.

Further, because of the physicality of the equipment, representing numbers of arbitrary length is hard.

So, two things are done to simplify the problem for computers to use.

First, there are already two symbols, 0 and 1 represented by no voltage and a voltage.

Rather than introducing a third symbol like a minus sign, a 1 is used to represent a minus and a 0 is used to represent a plus.

Second, instead of allowing an arbitrary number of digits, a fixed number of digits is defined.

Typically the number of digits are a power of two: 4, 8, 16 and so forth.

For the purposes of example, we'll assume 8 digits.

If we assume an unsigned number, 8 digits gives us a range of 0 to 255, or all zeros to all ones.

If we use a 1 in the most significant bit to represent a negative number and a 0 to represent a positive number, we reduce the number of bits for the number by one to 7.

This gives us a range of 0 to 127. With the last bit for a sign, the total range is 0 to +/- 127.

This introduces some interesting problems.

In this scenario, you have 0 to 127 in the positive range and then you add a 1 to the front for negative numbers.

Look what happens here at 0.

You have both a positive zero and a negative zero.

This doesn't make too much sense from a mathematical perspective.

The other problem is how to simply handle negative numbers?

First let's look at a decimal example to remember how borrowing works.

Let's take 2002 - 3.

We have 2 - 3, we can't do this so we try to borrow 1 from the previous column.

The previous column is already 0 so we have to borrow from its previous column.

Well this is also 0 so we do it again.

This means the two changes to a 1 and the third column is 10.

Now we borrow the 1 making the third column 9 and the second column 10.

Now we can borrow the original 1 we needed leaving the second column 9 and the first column 12.

Now the subtraction can occur with a final value of 1999.

Remember how this borrowing against 0 columns causes them to become 9s.

We'll come back to this.

Let's look at another example of 1 minus 2.

According to our earlier rule, we can't take a larger number from a smaller number.

So we make a new rule that says if we have a larger number from a smaller number, then swap the order and and add a minus sign to the result.

We could in theory do this in electronics.

Do a comparison, swap the order if needed and add the minus sign.

While it can be done, it's pretty complicated circuitry.

Let's look at what happens if we try doing some borrowing.

Using our example of 1 - 2, in binary we have 1 - 10, we can't do this so we have to borrow from a previous column.

But since the previous column is 0 so we go to the next column.

We do this until we run out of columns.

So we set the negative sign and make this last column 10, or the value 2.

Now we borrow one from that column, leaving 1 and shift to the next column.

This borrowing propagates all the way back leaving 1s in each column, just like we had 9s in our earlier decimal example.

Finally back to the first column, with the borrow, the subtraction can be done, leaving 1.

Because we have 0s in all the remaining columns, the borrows drop down, like the 9s did in our first decimal example.

This gives us a final value of 11111111.

If we wanted to make this look like the original idea of using the last bit for a sign and the remaining bits look just like their positive counterparts, somehow we need to make this look like 10000001.

We need to figure out how those 1s that are a result of borrowing get changed back to zeros.

It turns out this is a pretty complicated problem too.

But what happens if we just assign the result of all ones to mean -1?

Ultimately numbers are just arbitrary symbols with assigned values.

There's nothing stopping us from just saying this symbol represented by all 1s has the value -1.

If we do this, it really simplifies the electronic circuitry for the computer designers.

Let's take look at a couple other examples.

Let's take 1 - 3.

In binary this is 1 - 11.

Going through the process we used for 1 - 2, we end up with a final result of 11111110 which we assign the value of -2.

Now let's take 1 - 4. In binary this is 1 - 100.

Going through the process again, we end with a final result of 11111101 which we assign the meaning of -3.

Doing this again for 1 - 5 gives us 11111100 meaning -4.

Is there a pattern emerging?

If we change all the 1s to 0s and 0s to 1s we see this.

And this is the positive values offset by 1.

So there is a pattern and it's pretty logical.

And it turns out doing this offset by one and change of 0s to 1s and 1s to 0s is relatively easy to do in electronic circuitry.

It's called two's complement and has become the standard way of representing negative numbers.

Going back to our school days learning decimal numbers, once we learned about negative numbers, we then learned that all subtraction problems can be transformed into problems where we add the opposite.

So 2 - 1 becomes 2 + -1 and 1 - 2 becomes 1 + -2.

Well the same thing happens in binary using two's complement notation for negative numbers.

2 - 1 becomes 2 + -1.

Working through this, 0 + 1 is 1.

1 + 1 is 0 carrying a 1.

This 1 + 1 continues until we run out of digits leaving us with 00000001, the expected answer.

If we switch the operands, we have 1 - 2.

Converting this to adding the opposite gives us this.

Working through this 1 + 0 is 1.

The remaining columns are all 0 + 1 giving us 1s for a final result of 11111111 or -1.

Again the expected answer.

And it turns out this works for any combination of numbers.

Let's look at this on a number line.

And remember that weird case we had before with a positive zero and negative zero?

Well, it turns out with two's complement math, what used to be that hard to deal with negative zero just becomes -128.

Here at House of Hacks, I hope to inspire, educate and encourage people with a bent towards making technical and mechanical things out of wood, metal, cameras and electronics.

Binary is the basis for many things related to digital electronics, as well as Boolean algebra, which I'll cover in a future series.

If topics such as these are interesting to you, be sure to subscribe and click the bell notification icon to have YouTube alert you when future videos are uploaded.

Thanks for joining me on our creative journey.

Until next time, go make something. Perfection's not required. Fun is!

Friday, March 8, 2019

How to find lost camera gear - Set contact file (part 2)


Description

Ever lost a camera or other photo gear? Looking for ideas for how to find lost camera (digital)? This is the second in a series where Harley shows ideas that can help a lost camera find its way back home. These travel tips and hacks can help someone who has found a lost camera return it to you.

Other videos in this series: How to Find My Lost Camera
Photography videos: Photography Tutorials

Here at House of Hacks we do tutorials, project overviews, tool reviews and more related to making things around the home and shop. Generally this involves wood and metal working, electronics, photography and other similar things. If this sounds interesting to you, you may subscribe here.

If you’re interested in learning more about the House of Hacks' values, here’s a playlist for you.

And here’s the most recent video.

For a written transcript, go to How to find lost camera gear - Set contact file (part 2)

Music under Creative Commons License By Attribution 3.0 by Kevin MacLeod at http://incompetech.com.
Intro/Exit: "Hot Swing"

Transcript

Have you ever lost camera gear?

Are you looking for ideas to recover your camera gear if it ever does get lost?

Today at the House of Hacks, I'm going to talk about two such strategies.

Hi. If we're just meeting, welcome!

I'm Harley and this is the House of Hacks where we do projects related to the workshop, things made out of wood, metal, electronics and other related types of materials.

Today, it's photography gear.

I belong to a local photography Facebook group where occasionally somebody will run across some camera gear that was accidentally left at a popular shooting location.

Generally what will happen is a post will go out describing where it was found and who to contact for more information.

Often times the owner is a member of the group and gets their gear back.

Inspired by these posts, this is the second in a series to help reunite you and your gear if it does get lost.

There's a link to the series right up here.

Our cameras use SD or CF cards to store images on. These are really nothing more than solid state disks that can store any kind of information, not just images.

With this in mind, today's tip is to add a file that contains your contact information in it.

I'm going to show you two ways of doing this.

The first way is to create a text file.

To do this, open a text editor.

On Windows, hit the WIndows key, type "notepad" and hit enter.

On a Mac, hit Command-space and type "textedit" and then hit enter.

Once you're in the editor, put in your contact information. Things like your name, address, phone number, e-mail and website.

Then, save this information on your computer. It can be anywhere you'll remember where it is. Your desktop or documents folders are great locations.

Call the file something that makes the contents obvious. Something like "contact info" or "read me." If you start the name with an underscore character, it'll be at the top of the file list when sorted by name in Windows Explorer or the Mac Finder.

Next, plug your memory card into your computer using a card reader.

The operating system should detect the new drive. Using either Windows Explorer or the Finder, open the card's drive and copy the new file into it.

And you're done!

Do you have any strategies for helping your camera gear find its way home?

Leave a comment below. I'd love to hear it.

And the second way is to create an image with your contact information in it.

To do this, grab a blank sheet of paper. A sheet from your nearest printer will work great.

And then get a marker pen. One with a thick tip is best.

Just write your contact information on the paper and make sure it's legible.

Then take a picture of that page and you're done.

You can keep the paper in your camera bag and then whenever you reformat your card, it's really handy to just grab it and take a picture so that card is now ready to find it's way back home if it ever gets lost.

With either of these tips, if someone finds your camera or your card, all they have to do is take a look at it and they'll be able to find your contact information.

Like the tips in the last video, this is a great first step but it only works for your camera or memory cards. And it requires a bit of thought on the person finding your gear. They have to go look for the information.

And if you ever format the card, you have to put the information back, either copying it off the computer or taking another picture.

In the next tips, I'll give ideas for identifying your equipment that's less technical and doesn't require as much thought on the part of the person finding it. And it'll work for pretty much any item in your camera bag, not just cameras and memory cards.

I'll see you in one of these videos that YouTube thinks you'll enjoy.

And while making things, remember, perfection's not required. Fun is!