C++ variables are stored internally as so-called binary numbers. Binary numbers are stored as a sequence of 1 and 0 values known as bits. Most of the time, you don't really need to deal with numbers at the bit level; however, there are occasions when doing so is convenient. C++ provides a set of operators for this purpose.
The so-called bitwise logical operators operate on their arguments at the bit level. To understand how they work, examine how computers store variables.
The decimal number system
The numbers that you are familiar with are known as decimal numbers because they are based on the number 10. In general, the programmer expresses C++ variables as decimal numbers. Thus, you would say that the value of var is 123, for example.
A number such as 123 refers to 1 * 100 + 2 * 10 + 3 * 1. Each of these base numbers — 100, 10, and 1 — is a power of 10.
123 = 1 * 100 + 2 * 10 + 3 * 1
Expressed in a slightly different but equivalent way:
123 = 1 * 102 + 2 * 101 + 3 * 100
Remember that any number to the zero power is 1.
Other number systems
The use of a base number of 10 for the counting system stems, in all probability, from the fact that humans have 10 fingers, the original counting tools. The alternative would have been base 20.
If dogs had invented our numbering scheme, it may well be based on the numeral 8 (one digit of each paw is out of sight on the back part of the leg). Such an octal system would have worked just as well:
12310 = 1 * 82 + 7 * 81 + 3 * 8A0 = 1738
The small 10 and 8 here refer to the numbering system, 10 for decimal (base 10) and 8 for octal (base 8). A counting system may use any positive base.
The binary number system
Computers have essentially two fingers. (Maybe that's why computers are so stupid: Without an opposable thumb, they can't grasp anything. And then again, maybe not.) Computers prefer counting using base 2. The number 12310 would be expressed as:
12310 = 0*128 + 1*64 + 1*32 + 1*16 + 1*8 + 0*4 +1*2 + 1*1
= 011110112
It is always convention to express binary numbers by using 4, 8, 32, or 32 binary digits even if the leading digits are zero. This is also because of the way computers are built internally.
Because the term digit refers to a multiple of ten, a binary digit is called a bit. The terms stem from binary (b-) digit (-it). Four bits make up a byte. A word is usually either two or four bytes.
With such a small base, it is necessary to use a large number of bits to express numbers. It is inconvenient to use an expression such as 011110112 to express such a mundane value as 12310. Programmers prefer to express numbers by units of bytes, or four bits.
A single, four-bit digit is essentially base 16, because four bits can express up any value from 0 to 15. Base 16 is known as the hexadecimal counting system. Hexadecimal is often contracted to simply, hex.
Hexadecimal uses the same digits for the numbers 0 through 9. For the digits between 9 and 16, hexadecimal uses the first six letters of the alphabet: A for 10, B for 11, and so on. Thus, 12310 becomes 7B16.
123 = 7 * 161 + B (i.e. 11) * 160 = 7B16
Because programmers prefer to express numbers in 4, 8, 32, or 64 bits, they similarly prefer to express hexadecimal numbers in 1, 2, 4, or 8 hexadecimal digits even when the leading digits are 0.
Finally, it is inconvenient to express a hexadecimal number such as 7B16 using a subscript, because terminals don't support subscripts. Even on a word processor, it is inconvenient to change fonts to and from subscript mode just to type two digits. Therefore, programmers use the convention of beginning a hexadecimal number with a 0x (the reason for such a strange conviction goes back to the early days of C). Thus, 7B becomes 0x7B. Using this convention, 0x7B is equal to 123 while 0x123 is equal to 291.)
All of the mathematical operators can be performed on hexadecimal numbers in the same way that they are applied to decimal numbers. The reason that we can't perform a multiplication such as 0xC * 0xE in our heads has more to do with the multiplication tables we learned in school than on any limitation in the number system.
2006-06-28 15:27:08
·
answer #1
·
answered by BOYCUTE 2
·
1⤊
0⤋
It's about place value and powers of number. In decimal we go from 0 to 9 in binary it is 0 to 1.
In decimal the place values would be
Thousand Hundred Tens Ones
1111 = 1 thousand, 1 hundred, 1 ten and 1 one or 1,111
In binary the place values are
1024 512 256 128 64 32 16 8 4 2 1
1 0 0 0 1 0 1 0 1 1 1
1024+64+16+4+2+1= 1,111 in Decimal vlaues
The power grow by multiplyin the position by a factor of 2
1 x 2 = 2
2 x 2 = 4
2 x 4 = 8
2 x 8 = 16
2 x 16 = 32
2 x 32 = 64
2 x 64 = 128
2 x 128 = 256
2 x 256 = 512
2 x 512 = 1024
2 x 1024 = 2048
2 x 2048 = 4096
The binary system is arranged in groupings of 8, 16, 32, 64 and 128
8 groups (8 bits or places) is a byte
16 groups (16 bits or places) is a word or two bytes
32 groups (32 bits or places) is Long or Long word, sometimes called other things. It is four bytes.
The maximum number you can get with a byte is255 and this details a limitation you will see in programming, such as not being able to have more than 255 controls on a given form, because the systems keeps track of these in an 8 bit or byte level format.
The maximum number you can get with 2 bytes (16 bits) 65,535, which is another limitation you will see in programming. Old PCs had what was called a 64K barrier, because they worked with 16 bit registers and the biggest number of areas in memory they could see was 65,535 You will also see this in C++ You may be limited to 65,535 total controls on a given form.
The biggest decimal number you can get with 32 bits (four bytes) is 4,294,967,295.
That's 4.3 billion which was once a limitation on the size of hard drive or memory a PC could have, because it was limited to 32 bits, but they found clever ways to get around this limit.
We are now going into 64 bit processors with the new AMD and Intel offerings (the dual core processors).
This will go into terrabytes (trillions) and can directly calculate the nations debt.
The limitations here, is that if you try to calculate the national debt with 32 bits you will have some errors and be off by a few million.
The best way to understand binary is to construct a row and colum chart for dealing with up to at least 16, if not 32 bits, in decimal
32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
If your value is 66 it would be 0000000001000010 which is 64+2
A value of 9 would be 0000000000001001 which is 8+1
A value of 32770 would be 1000000000000010 which is 32768+2
2006-06-28 15:47:05
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋
Well for binary numbers there are several bases. But the most common base is base 2,
0110 =6 in base 2.
I came up with 6 because every digit is just a value of 2 raised to a power and then added together to get the decimal value.
0*(2^3)+1*(2^2)+1*(2^1)+0*(2^0).
To go from decimal to binary is a little different.
Division Quotient Remainder Binary Number
2671 / 2 1335 1 1
1335 / 2 667 1 11
667 / 2 333 1 111
333 / 2 166 1 1111
166 / 2 83 0 01111
83 / 2 41 1 10 1111
41 / 2 20 1 110 1111
20 / 2 10 0 0110 1111
10 / 2 5 0 0 0110 1111
5 / 2 2 1 10 0110 1111
2 / 2 1 0 010 0110 1111
1 / 2 0 1 1010 0110 1111
As you can see the remainder is the binary number and you divide by the previous quotient.
The second link has an excellent example.
2006-06-28 15:37:25
·
answer #3
·
answered by ReeseNe 2
·
0⤊
0⤋
in binary you can only have a 0 or 1, so to get the number 2 you have to do this 10. its NOT ten, its one zero.
in base 10 (normal numbers) 1101 would be calculated like this:
1000 100 10 1 (position value)
1 1 0 1
multiply the numbers by the position value then add the results up and you get 1000 + 100 + 0 + 1 = 1101
in binary its the same except the position values are different, and no need to multiply the numbers because 1 x Y = Y, and 0 x Y = 0. So you do this:
8 4 2 1 (position value)
1 1 0 1
just add up the ones with 1's 8 + 4 + 1 = 13.
notice that each position to the left doubles the value, so for 8 bits it would look like this:
128 64 32 16 8 4 2 1
2006-06-29 02:40:51
·
answer #4
·
answered by justme 7
·
0⤊
0⤋
A computer, in its simplest form, is nothing more than millions of switches (very FAST switches). They only have two states; on (1) or off (0). Hence, the development of Binary (meaning two), as well as the shadowing development of Hexidecimal (but that's another matter).
In Decimal (base-10), every space is 10 times the previous. Ones, tens, hundreds, thousands, etc. In Binary (base-2), every space is 2 times the previous. Ones, twos, fours, eights, sixteens, etc.
Visualize the following:
128-64-32-16-8-4-2-1
Here, we have 8 bits (one byte). Suppose you want to represent the number 174... 174 is greater than 128, so we place a 1 in the 128's space and subtract 128 from 174. Now, we have 46. 46 is less than 64, so we assign a 0 and move on. 46>32, so a 1 and subtract 32. 14<16, so another 0, etc... Now, we have:
10101110 binary = 174 decimal
To convert from binary back to decimal, just add them back together... 128+32+8+4+2 = 174.
Takes a bit of getting used to, but easily becomes second-nature after a while.
2006-06-28 15:33:01
·
answer #5
·
answered by twylafox 4
·
0⤊
0⤋
The numbering systen you're used to seeing is Decimal, or 10.
137 = 100 + 30 + 7
137 = 1 * (100) + 3 * (10) + 7 * (10)
Each consecutive digit to the left of the decimal point (137.00 = 137), multiplies itself by 10.
So take for example, the number 3547.0, that is 3 x 1000 + 5 x 100 + 4 x 10 + 7 x 1
Binary works the same way, but rather than using 10 as your base, you use 2.
101011 = 1x32 + 0x16 + 1x8 + 0x4 + 1x2 + 1x1 = 43
So, to convert from decimal to binary, you first need to know the decimal conversion table. As you saw above, decimal was easy, you just mulitply by 10... 10000, 1000, 100, 10, 1
Binary on the other hand, you multiply by 2...2048, 1024, 512 256, 128, 64, 32, 16, 8, 4, 2, 1 (you need to know, or at least know how to get to that).
So, to convert, start with the decimal number, lets go with our example of 137.
The largest number on our binary chart that fits into 137 is 128, so the first bit in the binary will be a 1.
1
Now, we subtract 128 from 137 (137 - 128 = 9). Now we need to check each binary number.
64 wont fit into 9
10
32 wont fit into 9
100
16 wont fit into 9
1000
8 WILL fit into 9 (now we subtract, 9 - 8 = 1)
10001
4 wont fit into 1
100010
2 wont fit into 1
1000100
1 WILL fit into 1 (lets subtract, 1 - 1 = 0, we're done)
10001001
This means that in binary, 137 = (1x128 + 0x64 + 0x32 + 0x16 + 1x8 + 0x4 + 0x2 + 1x1)
so you just try to make a combination of the table that fits, lets try another.
3547
2048 will fit into 3547 (3547 - 2048 = 1599)
1
1024 will fit into 1599 (1599 - 1024 = 575)
11
512 will fit into 575 (575 - 512 = 63)
111
256 will not fit into 63
1110
128 will not fit into 63
11100
64 will not fit into 63
111000
32 will fit into 63 (63 - 32 = 31)
1110001
16 will fit into 31 (31 - 16 = 15)
11100011
8 will fit into 15 (15 - 8 = 7)
111000111
4 will fit into 7 (7 - 4 = 3)
1110001111
2 will fit into 3 (3 - 2 = 1)
11100011111
1 will fit into 1 (1-1 = 0)
111000111111
And we're done with that one.
Here's a final example.
23
16 is the highest number that will fit into 23 (23 - 16 = 7)
1
8 wont fit into 7
10
4 will fit into 7 (7 - 4 = 3)
101
2 will fit into 3 (3 - 2 = 1)
1011
1 will fit into 1 (1-1 = 0)
10111
And we're done. Just remember, start with the biggest number on our table that fits into your decimal number. To convert from binary to decimal, just multiply each position by the right number in our table. Align it starting to the right.
Take binary number 1011010
512 256 128 64 32 16 8 4 2 1
1 0 1 1 0 1 0
so... 64x1 = 64
64 + 32x0 = 64
64 + 16x1 = 80
80 + 8x1 = 88
88 + 4x0 = 88
88+2x1 = 90
90 + 1x0 = 90
So, 1011010 is 90 in decimal. Try going from decimal back to binary.
2006-06-28 15:41:00
·
answer #6
·
answered by meb1337er 3
·
0⤊
0⤋
OK, here's the simple version.
Write your binary number in a column, starting from the right. So for 1101 you get:
1
1
0
1
Start at the bottom and write 1 next to the bottom digit
1
1
0
1___1
then go up the column, writing double the number below each time (i.e. put powers of two alongside).
1__8
1__4
0__2
1__1
Now erase the numbers that are next to a zero:
1__8
1__4
0__
1__1
Finally, add up the right column:
1__8
1__4
0__
1__1
--------
__13 ( = 1 + 4 + 8 )
Just to make it clearer, here's the method again for a longer binary number: 11100101101
Write it in a column with doubling numbers (i.e. powers of 2) next to it:
1__1024
1___512
1___256
0___128
0____64
1____32
0____16
1_____8
1_____4
0_____2
1_____1
Erase the numbers that are next to zeros:
1__1024
1___512
1___256
0
0
1____32
0
1_____8
1_____4
0
1_____1
and add up the remaining ones in the right column:
1+4+8+32+128+256+512+1024 = 1837
To convert from decimal to binary, write down your number, and the biggest power of two that is less than it:
13__8
Subtract the power of two, and write a 1 next to it:
13__8__1
_5
Now write the next smallest power of two next to the result. Is it less than or equal to the result? In this case, yes, so subtract again and write another 1.
13__8__1
_5__4__1
_1
Now write the next power of two. Is that less than or equal to the result? No, so don't subtract, but write a zero and copy the result to the next line:
13__8__1
_5__4__1
_1__2__0
_1
Now write down the next smallest power of two and repeat the steps above.
13__8__1
_5__4__1
_1__2__0
_1__1__1
_0
Now you've reached zero, so stop. The binary for your number can be read down the third column: 1101.
Again, an example with a bigger number:
1965__1024__1
_941___512__1
_429___256__1
_173___128__1
__45____64__0
__45____32__1
__13____16__0
__13_____8__1
___5_____4__1
___1_____2__0
___1_____1__1
___0
So 1965 in binary is 11110101101.
2006-06-29 07:50:38
·
answer #7
·
answered by Anonymous
·
0⤊
0⤋