# O.4 — Converting between binary and decimal

Consider a normal decimal number, such as 5623. We intuitively understand that these digits mean (5 * 1000) + (6 * 100) + (2 * 10) + (3 * 1). Because there are 10 decimal numbers, the value of each subsequent digit to the left increases by a factor of 10.

Binary numbers work the same way, except because there are only 2 binary digits (0 and 1), the value of each digit increases by a factor of 2. Just like commas are often used to make a large decimal number easy to read (e.g. 1,427,435), we often write binary numbers in groups of 4 bits to make them easier to read (e.g. 1101 0101).

The following table counts to 15 in decimal and binary:

Decimal Value Binary Value
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111

Converting binary to decimal

In the following examples, we assume that we’re dealing with unsigned integers.

Consider the 8 bit (1 byte) binary number 0101 1110. Binary 0101 1110 means (0 * 128) + (1 * 64) + (0 * 32) + (1 * 16) + (1 * 8) + (1 * 4) + (1 * 2) + (0 * 1). If we sum up all of these parts, we get the decimal number 64 + 16 + 8 + 4 + 2 = 94.

Here is the same process in table format. We multiply each binary digit by its digit value (determined by its position). Summing up all these values gives us the total.

Converting 0101 1110 to decimal:

 Binary digit 0 1 0 1 1 1 1 0 * Digit value 128 64 32 16 8 4 2 1 = Total (94) 0 64 0 16 8 4 2 0

Let’s convert 1001 0111 to decimal:

 Binary digit 1 0 0 1 0 1 1 1 * Digit value 128 64 32 16 8 4 2 1 = Total (151) 128 0 0 16 0 4 2 1

1001 0111 binary = 151 in decimal.

This can easily be extended to 16 or 32 bit binary numbers simply by adding more columns. Note that it’s easiest to start on the right end, and work your way left, multiplying the digit value by 2 as you go.

Method 1 for converting decimal to binary

Converting from decimal to binary is a little more tricky, but still pretty straightforward. There are two good methods to do this.

The first method involves continually dividing by 2, and writing down the remainders. The binary number is constructed at the end from the remainders, from the bottom up.

Converting 148 from decimal to binary (using r to denote a remainder):

148 / 2 = 74 r0
74 / 2 = 37 r0
37 / 2 = 18 r1
18 / 2 = 9 r0
9 / 2 = 4 r1
4 / 2 = 2 r0
2 / 2 = 1 r0
1 / 2 = 0 r1

Writing all of the remainders from the bottom up: 1001 0100

148 decimal = 1001 0100 binary.

You can verify this answer by converting the binary back to decimal:

(1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 148

Method 2 for converting decimal to binary

The second method involves working backwards to figure out what each of the bits must be. This method can be easier with small binary numbers.

Consider the decimal number 148 again. What’s the largest power of 2 that’s smaller than 148? 128, so we’ll start there.

Is 148 >= 128? Yes, so the 128 bit must be 1. 148 - 128 = 20, which means we need to find bits worth 20 more.
Is 20 >= 64? No, so the 64 bit must be 0.
Is 20 >= 32? No, so the 32 bit must be 0.
Is 20 >= 16? Yes, so the 16 bit must be 1. 20 - 16 = 4, which means we need to find bits worth 4 more.

Is 4 >= 8? No, so the 8 bit must be 0.
Is 4 >= 4? Yes, so the 4 bit must be 1. 4 - 4 = 0, which means all the rest of the bits must be 0.

148 = (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 1001 0100

In table format:

 Binary number 1 0 0 1 0 1 0 0 * Digit value 128 64 32 16 8 4 2 1 = Total (148) 128 0 0 16 0 4 0 0

Another example

Let’s convert 117 to binary using method 1:

117 / 2 = 58 r1
58 / 2 = 29 r0
29 / 2 = 14 r1
14 / 2 = 7 r0
7 / 2 = 3 r1
3 / 2 = 1 r1
1 / 2 = 0 r1

Constructing the number from the remainders from the bottom up, 117 = 111 0101 binary

And using method 2:

The largest power of 2 less than 117 is 64.

Is 117 >= 64? Yes, so the 64 bit must be 1. 117 - 64 = 53.
Is 53 >= 32? Yes, so the 32 bit must be 1. 53 - 32 = 21.
Is 21 >= 16? Yes, so the 16 bit must be 1. 21 - 16 = 5.

Is 5 >= 8? No, so the 8 bit must be 0.
Is 5 >= 4? Yes, so the 4 bit must be 1. 5 - 4 = 1.
Is 1 >= 2? No, so the 2 bit must be 0.
Is 1 >= 1? Yes, so the 1 bit must be 1.

117 decimal = 111 0101 binary.

In some cases (we’ll see one in just a moment), it’s useful to be able to add two binary numbers. Adding binary numbers is surprisingly easy (maybe even easier than adding decimal numbers), although it may seem odd at first because you’re not used to it.

Consider two small binary numbers:
0110 (6 in decimal) +
0111 (7 in decimal)

Let’s add these. First, line them up, as we have above. Then, starting from the right and working left, we add each column of digits, just like we do in a decimal number. However, because a binary digit can only be a 0 or a 1, there are only 4 possibilities:

• 0 + 0 = 0
• 0 + 1 = 1
• 1 + 0 = 1
• 1 + 1 = 0, carry a 1 over to the next column

Let’s do the first column:

```0110 (6 in decimal) +
0111 (7 in decimal)
----
1
```

0 + 1 = 1. Easy.

Second column:

``` 1
0110 (6 in decimal) +
0111 (7 in decimal)
----
01
```

1 + 1 = 0, with a carried one into the next column

Third column:

```11
0110 (6 in decimal) +
0111 (7 in decimal)
----
101
```

This one is a little trickier. Normally, 1 + 1 = 0, with a carried one into the next column. However, we already have a 1 carried from the previous column, so we need to add 1. Thus, we end up with a 1 in this column, with a 1 carried over to the next column

Last column:

```11
0110 (6 in decimal) +
0111 (7 in decimal)
----
1101
```

0 + 0 = 0, but there’s a carried 1, so we add 1. 1101 = 13 in decimal.

Now, how do we add 1 to any given binary number (such as 1011 0011)? The same as above, only the bottom number is binary 1.

```       1  (carry column)
1011 0011 (original binary number)
0000 0001 (1 in binary)
---------
1011 0100
```

Signed numbers and two’s complement

In the above examples, we’ve dealt solely with unsigned integers. In this section, we’ll take a look at how signed numbers (which can be negative) are dealt with.

Signed integers are typically stored using a method known as two’s complement. In two’s complement, the leftmost (most significant) bit is used as the sign bit. A 0 sign bit means the number is positive, and a 1 sign bit means the number is negative.

Positive signed numbers are represented in binary just like positive unsigned numbers (with the sign bit set to 0).

Negative signed numbers are represented in binary as the bitwise inverse of the positive number, plus 1.

Converting integers to binary two’s complement

For example, here’s how we represent -5 in binary two’s complement:

First we figure out the binary representation for 5: 0000 0101
Then we invert all of the bits: 1111 1010
Then we add 1: 1111 1011

Converting -76 to binary:

Positive 76 in binary: 0100 1100
Invert all the bits: 1011 0011

Why do we add 1? Consider the number 0. If a negative value was simply represented as the inverse of the positive number, 0 would have two representations: 0000 0000 (positive zero) and 1111 1111 (negative zero). By adding 1, 1111 1111 intentionally overflows and becomes 0000 0000. This prevents 0 from having two representations, and simplifies some of the internal logic needed to do arithmetic with negative numbers.

Converting binary two’s complement to integers

To convert a two’s complement binary number back into decimal, first look at the sign bit.

If the sign bit is 0, just convert the number as shown for unsigned numbers above.

If the sign bit is 1, then we invert the bits, add 1, then convert to decimal, then make that decimal number negative (because the sign bit was originally negative).

For example, to convert 1001 1110 from two’s complement into a decimal number:
Given: 1001 1110
Invert the bits: 0110 0001
Convert to decimal: (0 * 128) + (1 * 64) + (1 * 32) + (0 * 16) + (0 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 64 + 32 + 2 = 98
Since the original sign bit was negative, the final value is -98.

Why types matter

Consider the binary value 1011 0100. What value does this represent? You’d probably say 180, and if this were a standard unsigned binary number, you’d be right.

However, if this value was stored using two’s complement, it would be -76.

And if the value were encoded some other way, it could be something else entirely.

So how does C++ know whether to print a variable containing binary 1011 0100 as 180 or -76?

In case the section title didn’t give it away, this is where types come into play. The type of the variable determines both how a variable’s value is encoded into binary, and decoded back into a value. So if the variable type was an unsigned integer, it would know that 1011 0100 was standard binary, and should be printed as 180. If the variable was a signed integer, it would know that 1011 0100 was encoded using two’s complement (now guaranteed as of C++20), and should be printed as -76.

What about converting floating point numbers from/to binary?

How floating point numbers get converted from/to binary is quite a bit more complicated, and not something you’re likely to ever need to know. However, if you’re curious, see this site, which does a good job of explaining the topic in detail.

Quiz time

Question #1

Convert 0100 1101 to decimal.

Show Solution

Question #2

Convert 93 to an 8-bit unsigned binary number. Use both methods above.

Show Solution

Question #3

Convert -93 to an 8-bit signed binary number (using two’s complement).

Show Solution

Question #4

Convert 1010 0010 to an unsigned decimal number.

Show Solution

Question #5

Convert 1010 0010 to a signed decimal number (assume two’s complement).

Show Solution

Question #6

Write a program that asks the user to input a number between 0 and 255. Print this number as an 8-bit binary number (of the form #### ####). Don’t use any bitwise operators. Don’t use `std::bitset`.

Show Hint

Show Hint

Show Solution

 6.1 -- Compound statements (blocks) Index O.3 -- Bit manipulation with bitwise operators and bit masks

### 458 comments to O.4 — Converting between binary and decimal

• Jikojin

How I did it

• - Line 32: Limit your lines to 80 characters in length for better readability on small displays.
- Use single quotation marks for characters (Line 7).

Looks good! Alex can't use loops in the solution, because they haven't been covered yet.

• Peter Nguyen

I am confused in understanding why we should add 1 when we convert a binary two's complement to an integer. Also, in the example when we plus 1 to 1111 1111 to make it overflow to 0000 0000, but as my calculation, it should be 1 0000 0000 (correct me if I'm wrong).

If anyone can explain more explicitly the purpose of add 1 (instead of minus 1) when we convert back a binary two's complement to an integer it would help a lot.

You can either do -1 before flipping the bits or +1 after flipping the bits. It's up to you.

> make it overflow to 0000 0000
"Overflow" means leaving the representable range. In this example, we're limited to 8 bits.
1111 1111 + 1 = 1 0000 0000
But that's 9 bits, we only have 8, the most significant bit is dropped.

• Jonathan

Not fair.... All are using language that haven't teach yet. :(

• This isn't a competition, there will always be people who already know more than you. Don't worry, you'll learn about control flow too, and when you learn another language, you can be the one to use advanced code d:-)

• Jonathan

Haha just joking! C++ is quite fun to me coz I just learn for a few weeks.

Is there any improvement for the code? In line 39, the compiler told me that
'-=': conversion from 'double' to 'int', possible loss of data.
Is it a problem?

• * Line 23: Don't modify a variable while assigning to it. (Lesson 0.3.3).
* Line 37, 39, 47: Only use the conditional operator if you need its result. Use a regular if-statement otherwise.
* Line 37, 39: Double comparison of @degree to @i.
* Line 44 can be move out of the loop to prevent running the check in line 43 in every cycle.
(* Line 34 should be a for-loop.)

• Jonathan

thx.
For line 23, should I do it in this way instead?

And for line 37 and 39, will the workload for the compiler becomes larger if I use double comparison instead of an if/else statement?

• > Line 23
Same problem. You're modifying @degree while assigning to it.

> will the workload for the compiler becomes larger if I use double comparison
If your compiler doesn't optimize it, the work load at run-time is bigger.

• Harley Urquhart

i've been following along and have come up with a solution the final problem here im not at the for loop section yet but i have worked with code for a while. i would appreciate critique thanks

• * @num is unnecessary, you can work with @number.
* Use ++prefix unless you need postfix++. Line 6 can be merged with line 9.
* Line 11: Unnecessary.

• sasan.sn

Thank you very much.

• sasan.sn

Hello
I just wanted to thank you all for the hard work. The great value of your actions is reserved for me. I hope you succeed in doing so. I'm sorry that I could not use the "C ++ 17" standard. I will send my sample application. Please let us know if you have any comments about it. Certainly I will pay attention to it.
thank you again. +1

• You only need 1 CODE environment for your entire code, not for every line.

* Initialize your variables with brace initializers.
* Duplicate range check in @getNumber. Use a while (true) loop
* Declare only 1 variable per line.

• Michael

Is my code good?

• * Avoid abbreviations unless they're obvious.
* Initialize your variables with brace initializers.
* Use your editor's auto-formatting feature.
* Use ++prefix unless you need postfix++. Same for --.
* Line 53: No need for a comma-operator.

• Michael

I'm using code::block and code::block don't recognize variables initialized with brace initializers. And when I start a brace, it automatically place an ending brace, how do I skip past that ending brace or disable it? Since it's better to type in ' or " or ) or } or ] than going for the right arrow key. Sometimes they can be skipped using tab but sometimes I can't and pressing tab only gives me more trouble by indenting there.

• Alex

If brace initializers aren't working, you may not have enabled C++11, 14, or 17 functionality. I show how to do so in lesson "Installing an Integrated Development Environment (IDE)" (currently lesson 0.6)

• Gav

Completely new to programming but been enjoying the tutorial, really great content!

• Hi Gav!

* Only use double for floating point numbers.
* The return value of @bit is unused. Declare it void.
* Booleans are false/true, not 0/1.
* Avoid abbreviations unless they're obvious.

• Heisenberg

Hey Alex, Thanks for lesson.
Please give some feedback on my solution to problem 6.

• Rokki

is this what a object oriented program looks like ? :D

• Hi Rokki!

> is this what a object oriented program looks like
Nope, you'll learn about that later.

* Line 6: Initialize your variables with brace initializers.
* Line 74: Default-initialize variables or initialize them to 0 unless you need their value for something.
* Line 13-52: Repeated code is an indicator that you should use functions or loops. Have a look at the solution.

• Hi Alex!

• Dimbo1911

I "potatoed" this hard, so I wonder what you think about it (the usage of / instead of if while calculating 2 ints)

• Loops haven't been covered yet, so the solution can't make use of them.
If you wrote your code without the loop, you'd end up with the solution but with 1 extra operation (The division, because it replaces the string literals).

• Lorenz

I know we still not cover the for loops, but what about this as solution for the excercise? Any remarks?

• Hi Lorenz!

* Line 21: Initialize your variables with brace initializers.
* Line 21: Use --prefix unless you need postfix--
* @main: Missing return statement (?) (Code is missing).

Looks good otherwise.

• Lorenz

Thank you!

All 3 mistakes because I'm coming from C for microcontrollers, I will keep in mind!

• mbcas

This is my code for problem 6. If anyone can give me some feedback that'd be much appreciated! thank you.

• Hi!

* @readUserInput: Missing return-statement. Make sure you followed lesson 0.10 and 0.11.
* Avoid recursion (A function calling itself). You'll learn about alternatives later.

• Dong

- This is my Program. I use While to find the bit value by comparing with pow(2,l).

• Hi!

* Don't use "using namespace". It can lead to naming conflicts.
* Use ++prefix unless you need postfix++. It's faster.
* Don't use @system.
* Missing printed trailing line feed at the end of your program.
* @main can be moved below the other functions to avoid forward declarations.
* Inconsistent formatting. Use the auto-formatting feature of your editor.
* Line 14, 16, 28: Initialize your variables with brace initializers.
* C++ uses @std::flush or @std::endl to flush a stream. @fflush is C.
* Line 16: That's an array of vectors, not what you want. Read to lesson on @std::vector and arrays or keep your code simple until you get there.
* Line 18: @veca[8] doesn't exist. Behavior is undefined.
* @Binarybits: Missing return-statement. Make sure you followed lesson 0.10 and 0.11.

• Arthur

wondering if this is readable as is or should I be commenting it?

• If you need non-documentation comments, your code is poor (or very complex).

* Use the auto-formatting feature of your editor.
* Line 9: Initialize your variables with braced initializers.
* Line 8, 23, 24, 30: Should be constexpr.
* Line 8, 30: Duplicate.

Separate constants and code, add some space, and your code will be fine.

• Brennan

Just posting this to see if there's any way to improve, or weak points in the code. If you notice any I'd love to hear what I can do better

Out of curiosity, if somebody entered "a" instead of a number when prompted for it, what command could I use to trigger some of my own code?

Also I'm an idiot and tried to "log in" like 100 times before realizing I don't need an account to comment and I was actually trying to log
in as a site admin the whole time. So if an actual admin sees this, sorry about that

• Hi!

* Line 30: Initialize your variables with brace initializers. You used direct initialization.
* Line 5: Initialize your variables with brace initializers.
* Line 16, 17: Limit your lines to 80 characters in length for better readability on small displays.
* Don't use "using namespace".
* Don't use @exit.
* @std::cin.ignore without arguments will ignore only 1 character.

> if somebody entered "a" instead of a number
Extraction will fail. @std::cin.fail() will return true. Use @std::cin.clear() and @std::cin.ignore to bring the input stream back into a valid state.
This is also covered in a later lesson.

• Brennan

I hope this is a cleaner version. Could you elaborate "@std::cin.ignore without arguments will ignore only 1 character"?
I'm not sure if I understand the full use of @std::cin.ignore, aside from the fact it lets me use @std::cin.get()
so that I can view the results of my program without the console exiting upon completion. That's pretty much all
I know about @std::cin.ignore() and all I use it for as of right now.

In the future I'll try not to ask questions regarding future techniques, I should probably just finish all of
the lessons here so that nobody has to repeat themselves.

Thanks!

• * Line 3: Initialize to 0

Let's say you enter "123abc" in line 3. 123 is stored in @i, "abc" remains in the input stream. Line 4 will try to extract "abc", but it can't be stored in an int, so the stream enters a failed state.
@std::cin.ignore(std::streamsize count, char delim) (@std::streamsize is an integer type) ignores all characters until it finds @delim or @count characters have been ignored.
By default, @count is 1. 'a' would be removed from the input stream, by "bc" remains.
Ideally, all characters should be removed, so line 4 prompts the user to enter a second number. You can do this by passing @std::numeric_limits<std::streamsize>::max() and '\n'.

@std::cin.ignore disables the count check when @std::numeric_limits<std::streamsize>::max() is passed. It will now ignore (remove) all characters until it finds '\n' (It also removes '\n', but then stops).

• Nathan

Is this any good?

Also, for some reason /n wont work.

• Hi Nathan!

* Line 29: Initialize your variables with uniform initialization. You used direct initialization.
* Line 7: Initialize your variables with uniform initialization.

• Milosh

I'm trying to solve the last task for over than 2 hours now, but I can't find the problem. I think that the function insertNumber is making me problem but I don't how to change it.

• You're not using the value returned by @calculating.

* Initialize your variables with uniform initialization
* All three functions are missing a return statement
* Signed/unsigned mismatch
* Don't use "using namespace"
* 225 != 255

• Milosh

Wow, what a rush does hahaha. However Thank You :)

ps. sorry for the bad english, its not my native.
Well here's my solution.I think there are few places where i can improve it.
I have a few off-topic questions:
Why should we initialize our variables ( I got in habbit of doing it, which is good) when we can acutally default initialize (e.g. int number {} ).As far as I understood it will default initialize to 0 (depending of the type I think, in this case an int 0 ).Or thats just for C++11?
Also I just cant understand where should we use " std::endl " and where "\n".I just dont get whats direct output, I guess thats because I have no previous experience with programing, who knows.

Anyways thanks for this tutorials, Alex,they are great.Also thanks for this community for sharing their codes.I learned much from them aswell and thats why i post mine(and also to be corrected if I got it wrong ). :)
Also thanks for nascardriver hes extremely helpful!

• Hi!

> This is easy program and can be done with 2 functions or even 1
More functions usually mean better forward compatibility, reusability and maintainability.

> Why should we initialize our variables [...]
> As far as I understood it will default initialize to 0
It will. But if you explicitly initialize to 0, even a non-C++ developer can understand what's going on. Empty curly brackets might not be understood.

> Or thats just for C++11?
Uniform initialization was introduced in C++11. Before that, curly brackets could only be used to initialize lists.

> Also I just cant understand where should we use " std::endl " and where "\n"
When you use @std::endl, all output will be displayed in the console immediately.
When you use '\n', the output might stay in memory for a little while before it actually gets printed. @std::endl is slower than '\n'. Use '\n' unless you need immediate output.

> I think there are few places where i can improve it
* Only use conditional statements if you need to return value of the called functions. Use if-else-if-else otherwise.
* @calculateInBinary::status is unnecessary. You can pass false/true in line 68 and 73.
* Line 69: The value of @decimal_number is not used after this line. There's no reason to decrement it. A subtraction suffices.

Thanks for the reply nascardriver i really appriciate it,and implemented the suggestions.
Exept  that I dont get the last one.
> Line 69: The value of @decimal_number is not used after this line[...]

*But the next function is using the returned value of expression decimal_number - 2^n;

• You're right, the value is used. I should've said "@decimal_number is not used after this line".
This is what I meant

Hah, wow thats true.I really have no idea why I used arithmetic assignment instead of just decrement it.Thanks :)

• Moon Moon

How I did it (with for loop)

• Jon

Hello! Would writing the power function like this (with only 1 return function) cause any unseen problems? Seems to work OK:

int power2(int x, int power)
{
if (x >= power)
{
std::cout << "1";
x = (x - power);
}
else std::cout << "0";
return x;
}

• Hi Jon!

Nope, that's fine.

Please use code tags when posting code next time.

• Ángel

Here's my code:

• Hi!

* Line 8: Initialize your variables. Bugs caused by uninitialized variables are hard to debug
* Don't use "using namespace". It can cause name collisions.
* @main is missing a return-statement.

• Fulminata

As a point for readability. I would recommend not ending a sentence with a binary number and starting the next sentence with the same or another binary number. If someone happens to not see the period it can cause confusion in the interpretation.

ex. "Consider the 8 bit (1 byte) binary number 0101 1110. 0101 1110 means..."

It would be better as "Consider the 8 bit (1 byte) binary number 0101 1110. This binary number means..."

• Alex

Agreed, and updated. Thanks!

• Benur21

"In the following examples, we assume that we’re dealing with unsigned integers."

Integers with 1 byte?

• Alex

No, integers that are always positive. Revisit lesson 2.4.

• Benur21

But integers have 4 bytes (or 2 bytes minimum). I know unsigned doesn't have negative.

• Alex

It doesn't matter how big the integers are. The binary representation of a number doesn't change based on integer size (the high bits are just padded with 0s).

For example, with a 4-bit integer, the number 3 is represented as: 0011.
With an 8-bit integer, the number 3 is represented as 0000 0011.
With a 16-bit integer, the number 3 is represented as 0000 0000 0000 0011.

• Benur21

"For example, to convert 1001 1110 from two’s complement into a decimal number:
Given: 1001 1110
Invert the bits: 0110 0001
Convert to decimal: (0 * 128) + (1 * 64) + (1 * 32) + (0 * 16) + (0 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 64 + 32 + 2 = 98
Since the original sign bit was negative, the final value is -98."

Where is the sign bit?

• The first bit is the sign bit

• Benur21

The one that was inverted? Isn't it part of the number? Then why is unsigned 1 byte 0-255 and signed 1 byte 0-127?

• For unsigned numbers, the first bit is part of the number.
For signed numbers, the first bit is the sign bit.
Signed 1 byte is -128 to 127.

• Benur21

In this case, 1001 1110, 1 is the sign bit, and 001 110 is the number, right? Then the sign bit shouldn't be inverted and the (0 * 128) part should be removed, as it is not part of the number, it's the sign bit. Right? Because if it was still part of the number then we would have 0-255 instead of 0-127, which is wrong for a signed byte.

• > In this case, 1001 1110, 1 is the sign bit, and 001 110 is the number, right?
No. If the sign bit is set, you need to invert the entire number and add 1. Re-read the section about two's complement.

> Then the sign bit shouldn't be inverted and the (0 * 128) part should be removed
After inverting and adding 1, the first number could again be 1, but this time it's part of the number and no longer considered a sign.

> 0-127
There is no type with this range. It's either 0 to 255 or -128 to 127

• Benur21

"For example, to convert 1001 1110 from two’s complement into a decimal number:
Given: 1001 1110
Invert the bits: 0110 0001
Convert to decimal: (0 * 128) + (1 * 64) + (1 * 32) + (0 * 16) + (0 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 64 + 32 + 2 = 98
Since the original sign bit was negative, the final value is -98."

So when the byte is signed and the bit sign is 1, it will be able to be 0xxx xxxx, which will translate to a number between 0 and 127 (or 1 and 128 if you add 1). When bit sign is 0 (positive), it will be able to be 1xxx xxxx, which translates to between 128 and 255 (or 129 and 256 (0) if you add 1). So you can't have positive less than 128 (and bigger than 0)??

• > when the byte is signed and the bit sign is 1, it will be able to be 0xxx xxxx
But you add 1, so it's either 0xxx xxxx or 1000 0000, but at least 0000 0001, which is 1 to 128, or -1 to -128, because the sign bit was negative. If the sign bit is 0, the range is 0000 0000 to 0111 1111 or 0 to 127.

> When bit sign is 0 (positive), it will be able to be 1xxx xxxx
You only invert if the sign bit is 1. If it's 0, the number stays as it is.

• Benur21

> You only invert if the sign bit is 1. If it's 0, the number stays as it is.

Well, then the "(0 * 128)" is optional, as it will always be zero.
Got it, thanks for helping.

• 1000 0000
sign bit is 1 -> invert
0111 1111
1000 0000
that's -128, the step is not optional.

• Japlinho

Here is my code:

• Shawn

This is how I did it.

• Chirag

We can maybe cut down on the code by implementing arrays for each of the bits

• Piotr

I've wrote three functions: one for data input (unnecessary in this case), one for calculating the power of 2 and one for calculating and printing binary form and was so proud of myself. Then I've checked your solution and seen that it is so much more elegant and concise!

• Jean Pi

Hi!

Sorry if this outside the scope of this site, but I was wondering why we encode negative numbers this way.
If my understanding is correct, to write -5 in binary over a byte, we use 7 bits to write (-5 % 128) = 123, and add a 1 in front to indicate the number is negative. 0111 1011 is 123, so -5 is 1111 1011.
Ultimately, if we interpret the 1 in front as we do for unsigned numebrs, it's as if we replaced 251 by -5. So the sequence of numbers goes like this: 0,1,2... 127,-128,-127,... -1.

But doesn't this mean that -5 or any other negative number is not written the same way over 1 byte as it is over 2 bytes? Moreover, why do we use this particular sequence instead of another one? (for instance, 0,1,2...127,-1...-128, or even -128,-127,...127)

Agin, sorry to bother you with this, I realize it has nothing to do with actual coding. Thanks for your efforts!

• Hi Jean!

> If my understanding is correct
I don't know if your way of conversion is correct, here's how I learned it:

The standard doesn't specify a specific negative number representation. Compiler developers are free to use other representations. The most common representation is two's complement (What I did above)

> But doesn't this mean that -5 or any other negative number is not written the same way over 1 byte as it is over 2 bytes?
That's what it means.

> Moreover, why do we use this particular sequence
The sequence is determined by the binary representation. You always have

And convert that into decimal. Naturally, 0 is the first number. When using two's complement, the number following 127 (0111 1111) is -128 (1000 0000).

• Jean Pi

>That's what it means.

Humm, that's a tad strange. Thanks a lot!