Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 69
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
mileswaldron (59)


Next birthdays
06/03 mileswaldron (59)
06/04 muze801 (33)
06/05 HVgeek (33)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

Code puzzle

Move Thread LAN_403
Bjørn
Mon Jun 23 2008, 04:25AM Print
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
What does this reverse polish notation algorithm do? Each cell on the stack is 32 bit.

: PUZZLE 
  dup if dup signbit and 
  if abs 0x80000000 else abs 0 endif
  swap dup
  clz dup -rot ++ lsl 9 lsr
  31 rot - 
  127 + 23 lsl
  or or 
  endif ;

------------------------------------------------ ------------------------

Attempt 1, what happens if we run the code with the value 0 loaded on the stack? Every other line is the complete content of the stack and the operator that works on the top cells of the stack. Top of the stack is to the right.

0	
  dup
0 0	
  if
0	
  endif
0	
  ;
We are left with 0 so a no-operation.


That means we can ignore the code that handles the 0 and concentrate on the rest.
: PUZZLE 
  dup signbit and 
  if abs 0x80000000 else abs 0 endif
  swap dup
  clz dup -rot ++ lsl 9 lsr
  31 rot - 
  127 + 23 lsl
  or or 
  ;

------------------------------------------------ ------------------------
Back to top
Bjørn
Thu Jun 26 2008, 09:21AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
Attempt 2, what happens if we run the code with the value 1 loaded on the stack?

1
  dup 
1 1
  signbit 
1 1 0x80000000
  and  
1 0
  if 
1
  abs 
1
  #0 
1 0
  endif 
1 0
  swap
0 1
  dup 
0 1 1
  clz  ( count leading zeroes )
0 1 31
  dup 
0 1 31 31
  -rot
0 31 1 31
  ++ 
0 31 1 32
  lsl 
0 31 0
  #9 
0 31 0 9
  lsr
0 31 0 
  #31
0 31 0 31
  rot
0 0 31 31
  -  
0 0 0
  #127
0 0 0 127
  + 
0 0 127
  #23 
0 0 127 23
  lsl 
0 0 1065353216
  or 
0 1065353216
  or  
1065353216
  ;

We end up with 1065353216 or 0x3F800000 in hexadecimal. What is special with that number and what happened to the original 1 since 1065353216 is the constant 127 shifted left by 23?

--------------------------------------------- ---------------------------

Attempt 3, some answers...
If we do a search for 1065353216 we find that it is the decimal value of the 32 bit word that contains the value 1.0 in IEEE 754 floating-point format. In binary 00111111 10000000 00000000 00000000.

If we check the wiki we find the format for 32 bit floating-point numbers:

S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF
S - Sign
E - Exponent
F - Fraction

To find out if there is a connection we try with a more substancial number like 754. That gives us 01000100 00111100 10000000 00000000 in binary.

S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF
0 10001000 01111001000000000000000

An exponent of 136, since it i biased with 127 we get 136 -127 = 9 as the true exponent. The most significant bits of the fraction is:
011110010
and the binary of 754 is:
1011110010

So that is what happened to the 1 we tried earlier. The most significant bit of any non-zero number is always 1 so there is no point in wasting a bit of storage that can be used to increase precision instead.

The fraction is 23 bit long so to restore the original number we shift it (23 - Exponent) = 14 bits to the right and we get 011110010, we then stick back the implied most significant bit and get 1011110010 which is 754 in decimal.

So the code takes any signed 32 bit integer and converts it to IEEE 754 floating-point format.
Back to top
Steve Conner
Sat Jun 28 2008, 06:11PM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
Sorry Dr. Dreadlocks, your puzzle was just too hard.
Back to top
Bjørn
Sun Jun 29 2008, 01:42PM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
Yo, Dr. Skinhead,

How shall we as humans progress beyond the most basal understanding of our surroundings if we consistently aim for the mediocre? Even the smallest of children understand the basic operations of a stack, everyone should be familiar with the basic arithmetic operations or be able to look them up in their library, some but not all will be familiar with the concept of binary numbers. The rest is just a leap of faith and a determined mind.
Back to top
Alex
Mon Jun 30 2008, 12:53AM
Alex Geometrically Frustrated
Registered Member #6 Joined: Thu Feb 02 2006, 04:18AM
Location: Bowdoin, Maine
Posts: 373
Damn, I wish I had seen this earlier (or that I hadn't read straight through the answers). Interesting read, though. I have a fondness for RPN; my first programming experiences involved a FORTH-like language.
Back to top
Steve Conner
Mon Jun 30 2008, 02:49PM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
Can anyone guess what this one does?
void main(void)
{
   int x;
   float y;

   x = 123456; // assign a value to x
   y = (float)x; // convert x to floating-point
}
Back to top
Avi
Mon Jun 30 2008, 04:42PM
Avi Registered Member #580 Joined: Mon Mar 12 2007, 03:17PM
Location: Melbourne, Australia
Posts: 410
the same thing as the above one?
Back to top
Bjørn
Tue Jul 01 2008, 04:24AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
What it does in detail depends on the compiler and the target. If it is done in Visual C++ 2008 and the target is x86 then it would do the same thing, If the target is a microcontroller it might use a 16 bit int instead of 32 bit and it may use a different floating-point format than IEEE 754 or it may abort the compilation with an error.
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.