Shop OBEX P1 Docs P2 Docs Learn Events
2^32 — Parallax Forums

2^32

garylakegarylake Posts: 41
edited 2011-03-25 17:42 in Propeller 1
I was just wondering why the Prop can't calculate 2^32.
CON                              
                                 
  _clkmode = xtal1 + pll16x      
  _xinfreq = 5_000_000           
                                 
OBJ                              
                                 
  pst   : "Parallax Serial Terminal"

var
  long t
  long i
PUB main                                                  
  pst.Start(115_200)
  i := 2
  t:=0
  repeat 32
    t++
    pst.Dec(t)
    pst.tab
    pst.Dec(i)
    pst.newline   
    i := i *2

  pst.Str(string("All done!"))
Terminal window print out.
1       2
2       4
3       8
4       16
5       32
6       64
7       128
8       256
9       512
10      1024
11      2048
12      4096
13      8192
14      16384
15      32768
16      65536
17      131072
18      262144
19      524288
20      1048576
21      2097152
22      4194304
23      8388608
24      16777216
25      33554432
26      67108864
27      134217728
28      268435456
29      536870912
30      1073741824
31      -2147483648
32      0
All done!

Answer should be 4,294,967,296

Gary Lake

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-03-25 17:05
    How do you fit 2^32 into a 32bit entity? 2^n requires n+1 bits.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-03-25 17:05
    2^32 has overflowed.
    In binary the answer is
    1_00000000_00000000_00000000_00000000

    which is why you are getting zero. There will be an overflow flag somewhere.
  • Andrey DemenevAndrey Demenev Posts: 377
    edited 2011-03-25 17:07
    2^32 occupies 33 bits. In Spin, all calculations are done in 32 bits. Also, these are signed 32-bits, so integers must fall in -2147483648 .. 2147483647 range
  • garylakegarylake Posts: 41
    edited 2011-03-25 17:13
    I was just trying to do the math in the Propeller Education Kit. Labs: Fundamentals.

    Something like -- PHS bit 31 frequency = 80_000_000 x 322_122_547 / 2^32
  • Andrey DemenevAndrey Demenev Posts: 377
    edited 2011-03-25 17:22
    There is a special operatotor in Spin for this: **
    A ** B = A* B / 2^32. Basically, it does multiplication of two 32-bit numbers into 64-bit number, and returns the higher 32 of those 64 bits
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2011-03-25 17:24
    You could do this instead:

    Something like -- PHS bit 31 frequency = 40_000_000 x 322_122_547 / 2^31

    Duane
  • Andrey DemenevAndrey Demenev Posts: 377
    edited 2011-03-25 17:32
    This is completely wrong. First multiplication will result in overflow
  • garylakegarylake Posts: 41
    edited 2011-03-25 17:42
    @Andrey

    The A**B worked and the answer was 5,999,999

    Thanks
Sign In or Register to comment.