Author Topic: Decimal / Binary converter  (Read 8556 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Decimal / Binary converter
« Reply #30 on: July 11, 2021, 10:25:19 pm »
I meant to the precision of an Integer64. if it was used as the decimal precision.

What? Integer is integer and decimal precision means floats, go with String and have best of both worlds ;-))

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Decimal / Binary converter
« Reply #31 on: July 11, 2021, 10:40:26 pm »
You asked, "show me how to store this".

here if I make the rest of that line big and bold can you read it then?

I think we are just on 2 different planes here,

show me how to store this

in a BYTE, INTEGER, LONG, SINGLE, DOUBLE, or FLOAT?
Granted after becoming radioactive I only have a half-life!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Decimal / Binary converter
« Reply #32 on: July 11, 2021, 11:08:48 pm »
And, to show that these values are true and represented in memory in this manner, we have the following little program:

going off the page you shared, run this and see if you get the same number back they had,

from the page;
Quote
IEEE Short Real: 32 bits    1 bit for the sign, 8 bits for the exponent, and 23 bits for the mantissa. Also called single precision.

+1101.101       0  10000010  10110100000000000000000
so using SINGLE PRECISION (!)
with the 32 BITs as follows 01000001010110100000000000000000
printing the value, by your words, should print +1101.101 if it is represented in memory as a decimal-ed binary value.

Code: QB64: [Select]
  1. bit$ = "01000001010110100000000000000000"
  2. FOR i%% = 31 TO 0 STEP -1
  3.  IF MID$(bit$, i%%, 1) = "1" THEN a! = _SETBIT(a!, i%%)
  4. NEXT i%%
  5.  

alas it prints 46340
there is no native decimal information stored in that binary value. Which is why I don't believe we are talking about the same thing.
Granted after becoming radioactive I only have a half-life!

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Decimal / Binary converter
« Reply #33 on: July 11, 2021, 11:35:29 pm »
Thanks @Bert22306  we seem to have the same expression except you don't show a decimal. Since 27/101 is .2673 with bar over top to represent repeating 4 digits, I think .01... does it with decimal placement as:
.1 base 2 = 1/2
.01 base 2 = 1/4  or .25
.001 base 2 = 1/8 or .125
...

Yes, we get the same answer, and no, decimal points don't belong in binary numbers? I stipulated that the (decimal) value of the Most Significant Bit, in my answer, was 1. Which means that the next bit to the right would represent a value of 0.5, in decimal, etc. Which explains why the upper two bits are 0.

In my experience, that's the normal way of using binary numbers. You always have to specify either the value of the MSbit, or the value of the LSbit, to be able to use that binary number.

And yes, of course, decimal fractions can be encoded as binary numbers (responding to someone else, not you, bplus). Else, just one example, you'd never be able to use pi, in your programs!!

My decimal/binary/hex conversion program asks for the number of bits to be used in the binary, and asks for the value of the MSbit.

I have a different tool which deals only in binary. You tell it the value of the MSbit or the LSbit, and the number of bits in the binary number, and it gives the value of each bit in the binary number. No problem at all, representing decimal fractions in a binary number.

Okay, speaking of pi, this is what my program shows:

Decimal value =  3.141592653589793        Decimal as positive number = 3.141592653589793
MSbit value = 3 of the 32 -bit binary number

Binary value check, built from individual bit values (MSbit left) =
 10000110000010101001000111000001

Actual decimal equivalent =  3.14159265300259           Positive Integer = 2248839617             Hex = 860A91C1

You can, of course, change the scaling used. First, I create the binary number, within the limits allowed by _Float. And then, I go backwards, and reconstruct the original decimal value. You can see the error, in this 32-bit example. I could have specified fewer or more bits be used, in the binary expression, and a different decimal value assigned to the MSbit, of course. And "Positive Integer" means, what that binary number would represent, if it were to represent a decimal positive integer, i.e., where the LSbit represents decimal 1.
« Last Edit: July 12, 2021, 12:07:58 am by Bert22306 »

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Decimal / Binary converter
« Reply #34 on: July 12, 2021, 12:22:49 am »
You can also use what's known as fixed point, to represent decimal fractions in a binary number. Floating point gives you the most resolution, in general, but it's not mandatory, right?

Take a 16-bit binary number, in which you stipulate the the MSbit value, in decimal, is 5.0. This is the decimal value of each bit of that number. (So, when calculating the decimal value of the 16-bit number, you simply add up the decimal equivalent of every binary bit set to 1.)

---------------------------
Program computes the value of the bits in a digital quantity, starting with the known value of the MSbit
or the known value of the LSbit.

Bit numbers are assigned using the Internet convention (MSbit 0) and the serial convention (LSbit 0).

Value of each bit
Bit 0 or bit 15 =  5
Bit 1 or bit 14 =  2.5
Bit 2 or bit 13 =  1.25
Bit 3 or bit 12 =  .625     or 1 / 1.6
Bit 4 or bit 11 =  .3125    or 1 / 3.2
Bit 5 or bit 10 =  .15625   or 1 / 6.4
Bit 6 or bit 9 =  .078125   or 1 / 12.8
Bit 7 or bit 8 =  .0390625  or 1 / 25.6
Bit 8 or bit 7 =  .01953125               or 1 / 51.2
Bit 9 or bit 6 =  .009765625              or 1 / 102.4
Bit 10 or bit 5 =  .0048828125            or 1 / 204.8
Bit 11 or bit 4 =  .00244140625           or 1 / 409.6
Bit 12 or bit 3 =  .001220703125          or 1 / 819.2
Bit 13 or bit 2 =  .0006103515625         or 1 / 1638.4
Bit 14 or bit 1 =  .00030517578125        or 1 / 3276.8
Bit 15 or bit 0 =  .000152587890625       or 1 / 6553.6

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: Decimal / Binary converter
« Reply #35 on: July 12, 2021, 12:53:54 am »
@Cobalt @bplus @SMcNeill

I think what you are looking for is how the CPU does this.

Here is what I didn't want to explain, as to understand what the computer does, you need to understand assembler.

You see (in case you don't already know this) everything in a computer is stored in 1's and 0's. So when you do math in, say QB64, the CPU does it all in BINARY. That means fractions as well.

So, here is a quick pseudo-assembler routine to explain it.

In non-geek words, you need to split the binary number into three parts: integer, position of decimal place, and the fractional (decimal part) of the number.

From there (and you can follow the assembler program) you can do any math needed using 1's and 0's. (As you can see, this is done in BASE 2)

Code: Text: [Select]
  1. ;PROCEDURE EXPLANATION      
  2. ;This procedure converts the 4 digits of decimal fraction
  3. ;in to correspondent binary notation.  
  4.  
  5.  
  6. ;EXAMPLE DATA
  7. int_example  dw 0
  8. frac_example dw 1234        ;num is: 0.6425
  9.  
  10. mov ax, int_example
  11. mov bx, frac_example
  12. call convert
  13. end
  14.  
  15.  
  16. convert proc
  17.     call int_b_conversion
  18.     call frac_b_conversion
  19.     ret
  20. convert endp
  21.  
  22. int_b_conversion proc
  23.     ;to do
  24.     mov int_out, ax
  25.     ret
  26. int_b_conversion endp
  27.  
  28. frac_b_conversion proc
  29.     mov ax, 5000d
  30.     xor dx, dx           ;REG used to store the binary fractional part
  31.     mov cx, 08d          ;almost 8 digits in an half REG
  32.     cyc_01:  
  33.         rol dx, 1
  34.         cmp bx, ax
  35.         jg greater_so_sub
  36.         je break
  37.         ;lesser so multiply
  38.         rol bx, 1        ;mul by 2
  39.             ;have 0
  40.         jmp dodge_01
  41.         greater_so_sub:
  42.             inc dx       ;have 1
  43.             sub bx, ax
  44.             rol bx, 1
  45.         dodge_01:
  46.     loop cyc_01
  47.  
  48.     jmp dodge_02
  49.  
  50.     break:
  51.         sub cx, 1
  52.         inc dx
  53.         cyc_02:    
  54.             rol dx, 1
  55.             loop cyc_02
  56.  
  57.     dodge_02:
  58.             mov frac_out, dx
  59.             ret
  60. frac_b_conversion endp
  61.  



here if I make the rest of that line big and bold can you read it then?
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)