## Introduction

Carry can be a difficult concept to understand. Hopefully this page will help.

## Back to school

If you went to school in an English-speaking country, you might be familiar with the word in this context but it is still confusing. If you speak another language you probably have a word for it too.

For example, when adding two numbers:

```   27
+ 35
----
```

You can do this easily but consider it at a methodical level. We add seven to five to get twelve; we write a 2 and "carry" the 1 to the next column:

```   27
+ 35
----
2
1
```

Next we add two, three and the "carried" one to get six and hence the result:

```   27
+ 35
----
62
1
```

That's great. Now let's do it in binary.

```   %01
+ %01
-----
%10
1
```

There: we "carried" a 1 from bit 0 into bit 1 of the result. For a more realistic case:

```   %11111111
+ %00000001
-----------
%100000000
```

The problem here is that the result is bigger than 8 bits; so, we cut off the 1 at the start and store that in the carry bit. This tells us that there was a "carry out" of the high bit of the result and a "carry in" to the Carry flag.

## Borrowing

You may remember that at school there was something similar for subtraction; sometimes, you need to "borrow" from the next digit to have enough to make a positive result. Example:

```   43
- 15
----
```

You can't subtract five from three, so you "borrow" one from the next column; thirteen minus five is eight:

```    1
3 3
- 1 5
------
8
```

The next digit is then found by subtracting one from three and it is finished.

```    1
3 3
- 1 5
-----
2 8
```

In binary, much the same happens:

```   %110
- %101
------
%001
```

As we get up to eight bits, sometimes we need to "borrow" from an imaginary ninth column:

```   %00000000         %100000000
- %10000000       - %010000000
-----------       ------------
????????         %010000000
```

In these cases, we just "borrow" whatever we need to make it possible to get a result; the carry bit can tell whether anything was "borrowed" from the imaginary ninth bit. You can also describe it as a "carry in" to the high bit from the Carry flag.

## What that means in practice

Whenever an addition would make a result bigger then eight bits, the carry flag will become `1`; you can use this (with instructions like `adc`) to add that "carry out" onto another byte so you can effectively chain bytes together to make large numbers.

When a subtraction would make a negative number, the carry flag will become `1` and the result will instead be that negative number plus 256 (in signed notation; in two's complement notation it is a negative number).

When using the carry/no carry conditions with decrements in Z80 code, you need to remember that it becomes `1` on the transition from 0 to -1, which is one step later than the transition from 1 to 0 which triggers the zero condition. This can be useful or equally a tricky bug to discover.
{"ft_lang_label":"__label__en","ft_lang_prob":0.924597,"math_prob":0.85072607,"size":2805,"snap":"2021-31-2021-39","text_gpt3_token_len":716,"char_repetition_ratio":0.12174223,"word_repetition_ratio":0.018518519,"special_character_ratio":0.31301248,"punctuation_ratio":0.097345136,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9684759,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-18T03:40:35Z\",\"WARC-Record-ID\":\"<urn:uuid:7950a3b3-066b-4764-a8a2-1bf54368256b>\",\"Content-Length\":\"11144\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:332c6238-eb60-4dca-b9a2-e136f691e74e>\",\"WARC-Concurrent-To\":\"<urn:uuid:2db0fa3d-2131-420c-832d-8bc6172b204c>\",\"WARC-IP-Address\":\"172.67.201.203\",\"WARC-Target-URI\":\"https://www.smspower.org/Development/Carry\",\"WARC-Payload-Digest\":\"sha1:MTCW7ETD3HGX4IT7Y6L6EAJPB3JAZ2JF\",\"WARC-Block-Digest\":\"sha1:RDLLONWJCZKGXS3WTQBOCOPSS3VX3TG4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056297.61_warc_CC-MAIN-20210918032926-20210918062926-00673.warc.gz\"}"}