# C++

Discussion in 'Computer Science & Culture' started by Votorx, Jun 7, 2004.

Not open for further replies.
1. ### VotorxStill egotistic...Valued Senior Member

Messages:
1,126
I'm trying to tech myself C++ but I'm having trouble with hexdecimals and the Binary Numbering System.

I understand the concepts such as this:
1 = 1
2 = 10
3 = 11
4 = 100
5 = 101
6 = 110
7 = 111

and so on in Binary Code I also understand this:
10 = 0*64 + 0*32 + 0*16 + 1*8 + 0*4 + 1*2 + 0*1
100 = 1*64 + 1*32 + 0*16 + 0*8 + 1*4 + 0*2 + 0*1

Now, that's all simple and great. Only I completely lose it when I get into the hexidecimal system. Can anyone here explain the hexidecimal system to me? And how it relates to the Binary Number System?

This is what I know so far on that:
0-9 is expressed the same way in Hexidecimal as it is in the Binary Number System.
9 - 16 is expressed using the letters A-G
Hexidecimal's are a better way of storing information but how does this work? Can someone explain this equation to me?

123 = 7 * 16^1 + B (i.e. 11) * 16*^0 = 7B base16

Where the hell does the 7 come from? And why is B needed?

You know, most likely all I need is the equation to be explained, the rest I can probably get on my own.

Once you can answer that though I have another question. It has to do with performing Bitwise Logical Operations, but I will ask that after my first question is answered.

Thanks for your help.

to hide all adverts.
3. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
First of all, your declarations of 1=100 are not good code. 1 is a constant that you always want to have the same value. If you were allowed to set it to 100, you'd really be messing some things up. 1+2==3 would be false. It would equal 102 instead.

Don't understand your binary example either.

But, as to the question of hexadecimal, it's actually very simple. It's base 16 because that's how many digits it has. It counts from 0 to 15 before it adds a digit.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.

It is useful in binary because a nibble (4 bits) equals 16, well actually 15 but you have to add the 0 as a number. So each digit in hex equals 1/2 a byte. Two digits equals a full byte.

It's also helpful with binary because all you need to memorize is 0 through 15 in binary and you can translate any hex value into binary.

For instance, 0xFF is two nibbles with all their bits set. 1111 1111, 0xF0 is 1111 0000, 0xF1 1111 0001. Get it? And the 0x part is just to indicate that it's hex.

you take the number and divide by 16= 7.6875, so you know your big digit is 7. 7*16=112, 123-112=11, 11=B. Get it?

The hassle with hex is converting it Decimal. Converting to Binary is a piece of cake which is why it's useful.

Binary is a base 2. From right to left it goes. 2^0, 2^1, 2^2, 2^3, etc...
Hex is base 16, so it goes 16^0, 16^1, 16^2, etc...

Hope this clears things up. A good book for assembly language is the Art of Assembly by Randall Hyde, there's a free online version you can get at http://webster.cs.ucr.edu/. There's a free C++ tutorial too. It's a version of Beginning Visual C++ by Ivor Horton. Unfortunately, I can't find a link to it. Don't remember where I got it from, maybe Kazaa. I ended up buying the book so don't even know where the original file is to search for the name. I'll look for it and get back to you.

edit: just realized what you were saying with your 1=1 2=10, binary, doh! So ignore that part.

Last edited: Jun 7, 2004

to hide all adverts.
5. ### malkiriRegistered Senior Member

Messages:
198
Binary to decimal:
0110 = 0 * 2^3 + 1 * 2^2 + 1 * 2^1 + 0 * 2^0 = 6

Decimal "to" decimal:
291 = 2 * 10^2 + 9 * 10^1 + 1 * 10^0 = 291

Binary is base 2, which means there are 2 digits you can use to describe numbers - 0 and 1. Decimal, base 10, has 10 digits - 0 through 9. Hexadecimal is base 16 and has 16 digits - 0 through 9 and A through F. (note that there isn't a digit "G". Neither is there a digit "2" in base 2, or a digit "10" in base 10. The base signifies the number of digits...0-9 and A-G would be 17 digits.)

7B2F = 7 * 16^3 + 11 * 16^2 + 2 * 16^1 + 15 * 16^0 = 31535

Decimal to binary (this is one of several methods you can use to convert):
291 / 2 = 145 remainder 1
145 / 2 = 72 remainder 1
72 / 2 = 36 remainder 0
36 / 2 = 18 remainder 0
18 / 2 = 9 remainder 0
9 / 2 = 4 remainder 1
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1

So 291 in binary is 100100011.

3289 / 16 = 205 remainder 9
205 / 16 = 12 remainder 13 (D)
12 / 16 = 0 remainder 12 (C)

So 3289 in hexadecimal is CD9.

Does that help? There are many more explanations and examples out there.

to hide all adverts.
7. ### malkiriRegistered Senior Member

Messages:
198
The topic may be "C++", but this question doesn't deal strictly with C++. I think those were not code, but equivalences, with decimal on one side and binary on the other.

8. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
Yeah, I got that late. It takes a minute to sink in some times.

Please Register or Log in to view the hidden image!

9. ### VotorxStill egotistic...Valued Senior Member

Messages:
1,126
Ooohhhhh thank you, I needed something like this. Hope you don't mind if I print it out and use it as a reference

Please Register or Log in to view the hidden image!

Where did I do this?

Now I'm confused, I thought there weren't any 2 in Binary Number System. I thought it was just composed of 0s and 1s

Ahh I see. But what happens with the decimal? Is this just trumcated since its stored as an INT? Not a float?

Binary code is decimal to decimal like malkiri said

Here's another question. Does it matter how many bits there can be? How can a number above 255, like 300 be espressed in Binary Number System and Hexadecimal system?

10. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
You're right, I didn't see what you were saying with your initial declarations. I saw them as code in C++ rather than showing binary. Basically ignore that part.

And 1=100 was just an example, I thought you were talking C++ and couldn't remember exactly what you wrote so put in 1=100 as an equivalent to the statements you made. Sorry about the confusion.

11. ### VotorxStill egotistic...Valued Senior Member

Messages:
1,126
Oh, well sorry for that, I guess the topic is misleading. But it has to do with C++ so I thought that that would be the best topic.

12. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
Exactly the same way, just more digits. FF=255, FF FF=65535 and so on.

13. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
This is just a way of understanding how it works. Malkiri's method is more efficient. The way I did it I just ignored the decimal and subtracted the value of 7*16 from the whole to find the next digit.

14. ### VotorxStill egotistic...Valued Senior Member

Messages:
1,126
Oh I c. Well the example I got is very very poor. It came accross like you could only have 8 bits. If I were to say...have 12 bits in Binary Number System would that equal 1 1/2 bytes?

15. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
Yeah, F FF which would equal 4095. If all bits are set, that is.

16. ### VotorxStill egotistic...Valued Senior Member

Messages:
1,126
And this is hexidecimal right? Just clarifying. What about Binary?

As for the Bitwise Logical Operations, How does this work exactly? Assuming of course that I'm using the functions, \$ | and ~.

17. ### malkiriRegistered Senior Member

Messages:
198
These are all just different ways of representing the same number. Computers all store numbers in binary form, so no matter what representation in which you manipulate numbers in your code, they'll use up the same amount of storage.

Bitwise operators work as follows:

01101010 &
01001110
---------
01001010

01101010 |
01001110
---------
01101110

~ 01101010
---------
10010101

Each operator is defined by a truth table that determines what it does to the bits. For example, the truth table for the bitwise AND is:

Code:
  |0  1
--+----
0 |0  0
1 |0  1
This means that inputs of 0 and 0 to & produce 0. Inputs of 1 and 0 or 0 and 1 produce 0. Inputs of 1 and 1 produce 1. Contrast that with the bitwise OR:

Code:
  |0  1
--+----
0 |0  1
1 |1  1
Here, only inputs of 0 and 0 produce 0. Inputs of 1 and 0 or 0 and 1 produce 1.

The bitwise NOT operator (~) is a unary operator, and so only operates on a single input.

Code:
--+-
0 |1
1 |0
Bitwise operators (as opposed to logical or boolean operators) perform their operations on each individual bit, as you can see in the example above. Each pair of bits in the inputs is AND-ed or OR-ed together and the output is concatenated to the result.

Astute readers note that there are a number of other possible truth tables. In fact, there are 16 total possible 2 by 2 truth tables. This means that strictly speaking, there are 16 bitwise operators. Not all of them are terribly useful, however. The bitwise XOR ("exclusive or") truth table is below. Do you see why it's named "exclusive or"?

Code:
  |0  1
--+----
0 |0  1
1 |1  0

18. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
Here's the C++ tutorial I mentioned earlier. It's a visual C++ tutorial, so you need Microsoft Visual Studio 6 for it. http://mothweb.motherwell.co.uk/helpfiles/cscripthelpfiles/. It's the Wrxtutrl.chm file. Very good. But dedicated to MFC. The first half deals with general C++; then, it moves on to MFC.

19. ### VotorxStill egotistic...Valued Senior Member

Messages:
1,126
Okay, thanks alot for you help. I just have one last question and I'll be on my way. What exactly would Bit wise operators be used for?

20. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
Flags mostly. You use each bit to represent a condition you want to specify. The bitwise operators can set them to on and off through various methods. Bitmasks, etc...

21. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
Download that tutorial, it does a good job of explaining this stuff. It doesn't get bogged down with MFC til later in the tutorial (in case you don't have Visual Studio).

22. ### invert_nexusZe do caixaoValued Senior Member

Messages:
9,686
If you really want to learn some useful information about how a computer works, download the assembly language tutorial as well. Learning assembly explains a lot of how C++ does it's thing.

23. ### VotorxStill egotistic...Valued Senior Member

Messages:
1,126
OK, thanks alot