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.
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
;
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.
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.
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.
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.
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.