|
-
Aug 18th, 2000, 01:28 AM
#1
Thread Starter
Member
As we all no binary is a system of counting using only 0's and 1's. But how does this work? Well imagine an odometer (distance thingy in your car) that after it reaches a max number on one wheel it flips the one next to it one position. The base of the counting system denotes the number of positions on each. We use base 10 (ie. DECimal, DEC meaing 10) and there are 10 possible positions for our odometer 0,1,2,3,4,5,6,7,8,9. To convert a number from its base to base ten:
1) take a number
2) multiply its by its value
3) then by its base to the power of its' distance from the decimal point (NB the number to the left of the decimal point is distance 0, the one to the right is -1)
4) add it to a running total
5) do with all numbers
EXAMPLE USING HEXADECIMAL
number = A03D.C
1) A
2) A = 10
3) Base = 16
distance = 3
10 * 16^3 = 40960
4) Runing Total = 40960
1) 0
2) 0 = 0
3) base = 16
distance = 2
0 * 16^2 = 0
4) RT = 40960 + 0 = 40960
1) 3
2) 3 = 3
3) distance = 1
3 * 16^1 = 48
4) RT = 40960 + 48 = 41008
1) D
2) D = 13
3) distance = 1
13 * 16^0 = 13
4) RT = 356128 + 208 = 41021
1) C
2) C = 12
3) distance = -1
12 * 16^-1 = 0.75
4) RT = 41021.75
Therefore A03D.C = 41021.75
(check it on your WinCalculator)
you can see that it is neater and shorter.
for other bases replace 26 with the appropriate base #.
Please respond if you found this helpful or want clarification.
Visual Basic 6 Professional Edition
Captain Pinko
also:
Turbo Pascal, Turing, QBasic
-
Aug 18th, 2000, 02:03 AM
#2
transcendental analytic
Here's something you could have use for, you could convert from between whatever bases. The elements used beyond 9 continues on A-Z and [\] and so on
Code:
Function Base2Dec(val$, base%): Dim n%, a() As Byte
a = StrConv(UCase(val), vbFromUnicode)
For n = 1 To Len(val)
Base2Dec = CDec(Base2Dec + (a(n - 1) - 48 + 7 * (a(n - 1) > 64)) * base ^ (Len(val) - n))
Next n
End Function
Function Dec2Base$(val, base%): Dim n%, s
For n = 0 To 100
s = Int(val / base ^ n)
If s = 0 Then Exit For
s = s - Int(s / base) * base 's mod base
Dec2Base = Chr(s + 48 - 7 * (s > 9)) & Dec2Base
Next n
End Function
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 18th, 2000, 02:14 AM
#3
cool, I have been looking for code like that for a long time
-
Aug 18th, 2000, 08:59 AM
#4
Addicted Member
I think it might be usefull to understand the base numbers in binary
1 - 1
2 - 10
4 - 100
8 - 1000
16- 10000
32- 100000
64- 1000000
128-10000000
256-100000000
512-1000000000
1024-10000000000
each binary increases to another digit
you add one to the right, you move it up until you get to the end for example (i'm only going to do the first few)
1
10
11
100
101
110
111
1000
1001
1010
1100
1101
1110
1111
10000
also keep note all the way upto 256 , hexidecimal remains 2 digit ( Octal stops at 8 )
hexidecimals are used mainly in representation, because the digits can remain small even if the numbers get big.
1 - 1 - 1
2 - 10 - 2
4 - 100 - 4
8 - 1000 - 8
16 - 10000 - 10
32 - 100000 - 20
64 - 1000000 - 40
128 - 10000000 - 80
256 - 100000000 - 100 ( 255 is FF, numerically it goes from 0-FF for 0-255, which still makes a count of 256)
see the patern in each base?
up til 255 Hex keeps a consitant 2 digit representation.
so for each binary step it adds a digit, for each hexidecimal step, it doubles itself(or would seem like it doubles itself if you looked at it from a 10base perspective)
but the source codes above are good if you just want to get the representation conversion without understanding it all (btw I did not use a calculator or anything, it's from memory, if you want to check my work go ahead)
-
Aug 18th, 2000, 08:40 PM
#5
Thread Starter
Member
Thanx for the conversion code....
Thanx for replying but the point was to explain how they work. And as for memorizing the binary it is not all that hard. Just take number that is two to the power of XXXXX and stick a 1 in that spot (eg. 512 = 2^9 so = 100000000 in binary)
Visual Basic 6 Professional Edition
Captain Pinko
also:
Turbo Pascal, Turing, QBasic
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|