<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Polemian vzw</title>
	<atom:link href="http://polemian.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://polemian.org</link>
	<description>social, cultural and ecologic informatics</description>
	<pubDate>Sat, 06 Mar 2010 11:12:48 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Multimedia in GNU/Linux - Capturing</title>
		<link>http://polemian.org/multimedia-in-gnulinux-capturing/</link>
		<comments>http://polemian.org/multimedia-in-gnulinux-capturing/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 10:08:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[GNU/Linux]]></category>

		<guid isPermaLink="false">http://polemian.org/?p=59</guid>
		<description><![CDATA[The Video for Linux project develops drivers for analogue video capture hardware like TV-cards. Using these drivers you can watch en record TV programs and VHS tapes on your computer. Most capture cards have multiple inputs leaving you the choise which one to use. Most common video inputs are TV/FM in (RF), composite and s-video. Audio inputs should be 2 RCA ports for stereo sound.]]></description>
			<content:encoded><![CDATA[<p><strong>CAPTURING WITH VIDEO FOR LINUX (V4L).</strong></p>
<p>The <strong>Video for Linux</strong> project develops <strong>drivers</strong> for analogue video capture hardware like <strong>TV-cards</strong>. Using these drivers you can watch and record TV programs and VHS tapes on your computer. Most capture cards have multiple inputs leaving you the choise which one to use. Most common video inputs are TV/FM in (RF), composite and s-video. Audio inputs should be 2 RCA ports for stereo sound.</p>
<p>Video for Linux has 2 main versions: <strong>v4l</strong> and <strong>v4l2</strong>. In this text we will be using v4l2 since it&#8217;s the latest and superior version. However when reffering to v4l2 we will use the abbreviation v4l.</p>
<p>In order to control the v4l driver we will use the <strong>v4l2-ctrl</strong> utility (/usr/bin/v4l2-ctrl). The v4l2-ctrl options will be discused along the article. The first options we will use are the &#8216;&#8211;*-video-input&#8217; options. These options list, set and get the input port. Listing 1 shows the 3 options (the output for &#8211;list-video-inputs is wrapped with tree dots.</p>
<pre><strong>$ v4l2-ctrl --list-video-inputs</strong>
ioctl: VIDIOC_ENUMINPUT
Input   : 0
Name    : Tuner 1
...
Input   : 1
Name    : S-Video 1
...
Input   : 2
Name    : Composite 1
...
Input   : 3
Name    : S-Video 2
...
Input   : 4
Name    : Composite 2
...</pre>
<pre><strong>$ v4l2-ctrl --set-video-input=2</strong>
Video input set to 2 (Composite 1)</pre>
<pre><strong>$ v4l2-ctrl --get-video-input</strong>
Video input : 2 (Composite 1)</pre>
<p><strong>Tuner (TV and Radio).</strong><br />
In this article we will not use the TV/Radio port, nor the posibilities when watching TV.</p>
<p><strong>Composite video.</strong><br />
Composite video is most commonly distributed over <strong>RCA ports</strong> and cables and therefore of lesser quality the newer connectors like s-video and scart. Composite video has tree components: 1 for <strong>luminance</strong> (brightness) and 2 for color information. The 2 color components are mixed together to create the <strong>chrominance</strong> (color) component. The first component by itself would output a monochrome picture. All components are transmitted on one wire.</p>
<p><strong>S-Video.</strong><br />
The s-video standard is an extension of composite video. It separates the chrominance from the luminance by using different wires and thus leaving more bandwidth, hence more picture quality.</p>
<blockquote><p><em>Note on s-video:</em><br />
<em>Most TV/VCR devices do not handle s-video correctly because the SCART specification (EURO-AV) does not implements ports for the s-video components, instead s-video over scart uses the RGB ports resulting that with some devices the image will be monochrome as only the luminance component is transmitted. If you need the quality of s-video, you will probably need to aquire an s-video device. JVC&#8217;s website is a good start.</em></p></blockquote>
<p>In order to view the signal passed trough v4l we can use a commandline media player like mplayer. We just need to play the contents that v4l gets from the capture card, which usualy is <strong>mpeg-2</strong> encoded. The content is a constant video stream, with, depending on the hardware, just a few seconds of delay because it is encoded on the fly.</p>
<p>The v4l devices are located in /dev/v4l but there should also be links inside the /dev directory. In the /dev/ directory there is also a direct link to the common video device named &#8216;video&#8217;. We will use this device to access the video stream.</p>
<pre><strong>$ ls /dev/v4l</strong>
radio0 vbi0 video0 video24 video32</pre>
<pre><strong>$ ls -al /dev |grep v4l</strong>
lrwxrwxrwx  1 root root          10 Apr 17 13:10 radio -&gt; v4l/radio0
lrwxrwxrwx  1 root root          10 Apr 17 13:10 radio0 -&gt; v4l/radio0
drwxr-xr-x  2 root root           0 Apr 17 13:10 v4l
lrwxrwxrwx  1 root root           8 Apr 17 13:10 vbi -&gt; v4l/vbi0
lrwxrwxrwx  1 root root           8 Apr 17 13:10 vbi0 -&gt; v4l/vbi0
lrwxrwxrwx  1 root root          10 Apr 17 13:10 video -&gt; v4l/video0
lrwxrwxrwx  1 root root          10 Apr 17 13:10 video0 -&gt; v4l/video0
lrwxrwxrwx  1 root root          11 Apr 17 13:10 video24 -&gt; v4l/video24
lrwxrwxrwx  1 root root          11 Apr 17 13:10 video32 -&gt; v4l/video32</pre>
<pre><strong>$ mplayer -cache 8192 -ni /dev/video</strong></pre>
<p>This last command should play the video stream from the selected source in listing 1. Remember that there is a short delay (a few seconds). The video source should be started first. The v4l device will return a black (or blue, if the device is in standby position) screen until the picture is send trough. Making mplayer cache before it plays the stream is recommended, otherwise it might complain. The -ni option makes mplayer parse the stream as non-interleaved, which also clears some errors.</p>
<p>Now that we have selected the right video source we can start setting up the v4l sound and picture options.</p>
<p><strong>CAPTURING WITH THE IEEE1394 ( FIREWIRE / ILink ) DRIVERS.</strong></p>
<p>The <strong>ieee1394 </strong>project develops <strong>kernel modules</strong> (drivers) for <strong>firewire</strong> or<strong> i.Link</strong> harware (firewire and i.Link are the commercial names given to the ieee1394 protocol by Apple and Sony). There are no v4l devices for ieee1394, one can however use the v4l vloopback device but that is not within the scope of this text. In this text the video1394 module will be used instead.</p>
<p>The video1394 module uses a device named /dev/video1394, or /dev/video1394-0 where 0 indicates the current controller in use. (the module supports up to 4 different controllers or cards). The module may not be loaded by default, to check if it is loaded run &#8216;lsmod |grep video&#8217; This should return (depending on which modules are loaded) something like:</p>
<pre><strong>$ lsmod |grep video</strong>
video1394              18448  0
ohci1394               33608  2 video1394,dv1394
ieee1394               93144  4 video1394,dv1394,raw1394,ohci1394
videodev               27904  1 ivtv
v4l2_common            18688  6 wm8775,cx25840,tuner,ivtv,cx2341x,videodev
v4l1_compat            13956  2 ivtv,videodev</pre>
<p>If the video1394 module is not listed, load it with modprobe, then run &#8216;lsmod |grep video&#8217; again.</p>
<pre><strong>$ modprobe video1394</strong>
<strong>$ lsmod |grep video</strong></pre>
<p>Now check if the video1394 device is created properly with &#8216;ls /dev/*1394*&#8217;. This should return a list of ieee1394 devices similar to the next example depending on the ieee1394 modules that are loaded.</p>
<pre><strong>$ ls /dev/*1394*</strong>
/dev/raw1394
/dev/dv1394
/dev/video1394-0</pre>
<p>These devices are created following the conventions given in the <strong>udev ruleset</strong> (when running udev) or the <strong>devfs config file</strong> (when using devfs).</p>
<p>Further reading (and searching):<br />
ENCODING MPEG-2 (DVD) WITH MENCODER.<br />
DVD AUTHORING WITH DVDAUTHOR AND SPUMUX.</p>
]]></content:encoded>
			<wfw:commentRss>http://polemian.org/multimedia-in-gnulinux-capturing/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Een korte inleiding tot GNU / Linux.</title>
		<link>http://polemian.org/korte-inleiding-gnu-linux/</link>
		<comments>http://polemian.org/korte-inleiding-gnu-linux/#comments</comments>
		<pubDate>Wed, 07 May 2008 11:48:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[GNU/Linux]]></category>

		<guid isPermaLink="false">http://localhost/~psyched_weels/site2009/?p=14</guid>
		<description><![CDATA[GNU / Linux is een modern besturingssysteem, net zoals Microsoft Windows XP, Vista en Apple OS X. De term Linux word vaak als verkorte naam gebruikt, maar is eigenlijk foutief in die zin dat linux zelf enkel de (meest gebruikte) kernel (een kernel is een basis onderdeel van een besturingssysteem) voor het GNU besturingssysteem is. Het GNU besturingssysteem heeft zijn eigen kernel maar deze is veel minder populair. Het grootste deel van het GNU/Linux besturingssysteem bestaat dus uit de GNU toolset.]]></description>
			<content:encoded><![CDATA[<p>GNU / Linux is een modern besturingssysteem, net zoals Microsoft Windows XP, Vista en Apple OS X. De term <strong>Linux</strong> word vaak als verkorte naam gebruikt, maar is eigenlijk foutief in die zin dat linux zelf enkel de (meest gebruikte) <strong>kernel</strong> (een kernel is een basis onderdeel van een besturingssysteem) voor het GNU besturingssysteem is. Het <strong>GNU besturingssysteem</strong> heeft zijn eigen kernel maar deze is veel minder populair. Het grootste deel van het GNU/Linux besturingssysteem bestaat dus uit de GNU toolset.</p>
<p><span id="more-14"></span></p>
<h3>De GNU Toolset.</h3>
<p>De GNU toolset is een verzameling van vele kleinere programma&#8217;s die allemaal een specifieke rol spelen binnen een besturingssysteem. Het anagram GNU staat voor GNU is Not Unix, waarmee men doelt op het feit dat, ondanks het GNU systeem en dus ook het GNU/Linux besturingssysteem gebaseerd is op het Unix besturingssysteem, het een vrij systeem is. Werken met Unix is enorm beperkt door patenten en licentievoorwaarden maar door zijn modulaire aard kan men de onderdelen &#8216;vervangen&#8217; door vrije GNU varianten. Andere besturingssystemen die op Unix gebaseerd zijn, zijn ondermeer OS X, Solaris en (Free)BSD.</p>
<h3>Waarom Unix?</h3>
<p>Waarom baseert men zich op een systeem van eind jaren &#8216;70? Het <strong>Unix</strong> bestandssysteem dat de dragende kracht is van het platform, bepaalt dat zo goed als alles een onderdeel is in een grote boomstructuur (vanuit het perspectief van het systeem). Of het nu om een tekst gaat of een toetsenbord of printer, in principe is alles een bestand. Op die manier is het systeem modulair en dus beter te beheren. Unix bestaat ook uit vele kleine (niet-interactieve) programma&#8217;s die samenwerken om taken uit te voeren. De combinatie van deze kleine programma&#8217;s bepaalt dus de functionaliteit. Dus opnieuw modulariteit. Het systeem laat ook meerdere gebruikers toe en heeft dus een zeer uitgebreid gebruikersrechtensysteem, wat van uiterst belang is voor de veiligheid bij het werken binnen een netwerkomgeving zoals bijvoorbeeld het internet.</p>
<h3>Distributies.</h3>
<p>Door uiteenlopende meningen en dankzij de open aard van GPL (General Public License) gelicenseerde software zijn er verschillende versies ontstaan van het GNU/Linux besturingssysteem. Zulke versies worden <strong>distributies</strong> genoemd. Eigenlijk wordt een GNU/Linux systeem getypeerd door de manier van configuratie en de meegeleverde software. Er zijn bijvoorbeeld distributies die zich specialiseren in bepaalde systeemvereisten zoals die van laptops, GPS systemen, game consoles, servers of desktop computers. Andere distributies leggen zich dan weer toe op specifieke gebruikersnoden zoals multimedia, geluidssynthesis, geografische doeleinden, forensisch onderzoek, specifieke taalondersteuning enzovoort.</p>
<p>Distributies worden zowel door privé-personen als door communities (mensen die via het internet met elkaar samenwerken) ontwikkeld, maar evengoed door universiteiten, overheidsinstellingen en -diensten. De meeste grote - en dus succesvolle - distributies staan echter op naam van bedrijven of zijn door bedrijven opgekocht. Bekende distributies zijn ondermeer IBM&#8217;s <strong>Redhat Linux</strong>, <strong>Novell Suse Linux</strong>, <strong>Mandriva</strong>, <strong>Debian GNU/Linux</strong> en <strong>(k)Ubuntu Linux</strong>. De meeste linux distributies stammen af, of zijn bewerkingen van deze grote distributies en delen dan ook heel wat eigenschappen met deze distributies. Er is echter een nieuwe generatie distributies die ontwikkeld wordt aan de hand van <strong>LFS</strong> (Linux From Scratch): een distributie die aan de hand van een uitvoerige handleiding van nul af aan stap voor stap een puur GNU/Linux systeem opbouwt. Deze worden dan stelselmatig aangepast aan de eisen van een modern linux systeem. Een voorbeeld van een moderne distributie is <strong>Archlinux</strong>.</p>
<h3>Wat is de beste distributie?</h3>
<p>Hierop is geen eenduidig antwoord te geven: de ene distributie is beter voor de ene taak, een andere is dan weer geschikter voor een andere. Ook mensen verschillen in hun voorkeur voor een bepaalde distributie. De hoofdregel is: elke distributie is in principe compatibel. Men kan dus meerdere systemen binnen dezelfde werkomgeving gebruiken zonder al te veel aanpassingen en complicaties. Het komt er dus op neer om te bepalen wat de eisen zijn die de gebruiker(s) en de omgeving stellen. Enkele belangrijke elementen in deze bepaling kunnen zijn: de middelen (De eisen die oudere computers stellen zijn anders dan de eisen van laptops, die dan weer anders zijn dan die van servers&#8230;), de gebruikers (ervaren of niet-ervaren gebruikers) en het beheer (onderschat nooit de tijd die noodzakelijk is om een systeem degelijk te beheren; kies een systeem dat voldoende door zijn gebruikers kan beheerd worden).</p>
<h3>Wat kan ik wel / niet doen op een GNU/Linux systeem?</h3>
<p>De mogelijkheden van het systeem zijn in zekere mate bepaald door de distributie enerzijds en het bestaan van de gewenste functionaliteit voor het GNU/Linux systeem anderzijds. Bepaalde programma&#8217;s stellen eisen aan het besturingssysteem waardoor het voor andere programma&#8217;s moeilijker wordt om te functioneren. Zoals bijvoorbeeld de specifieke aanpassingen die noodzakelijk zijn om betere beeld- of geluidsbewerking te doen. Over het algemeen is er een goede basisconfiguratie die het de meeste programma&#8217;s mogelijk maakt te werken. De vraag blijft natuurlijk of er een bepaalde functionaliteit voorhande is voor het GNU/Linux systeem. Maar dan nog blijft de vraag: is het programma dat deze functionaliteit implementeert voldoende stabiel en voldoet deze aan de eisen van de gebruiker. De meeste geavanceerde programma&#8217;s die vrij (en dus gratis) beschikbaar zijn voor GNU/Linux zijn in constante ontwikkeling en kunnen dus kinderziekten vertonen. Voor de meest voor de hand liggende noden echter, zijn er voldoende programma&#8217;s die goed functioneren. Bij programma&#8217;s voor bureautica- en multimediatoepassingen komt het er dus meestal op neer te kiezen, eerder dan te vinden. Op het internet kan men via zoekrobots zoals Google meestal snel een GNU/Linux variant vinden op een programma door te zoeken naar <em>&#8220;programmanaam+linux&#8221;</em>.</p>
<h3>De General Public Licence of GPL.</h3>
<p>GNU/Linux en de meeste programma&#8217;s (software) die beschikbaar zijn voor GNU/Linux, maar ook veel programma&#8217;s die beschikbaar zijn voor andere besturingssystemen vallen onder het <strong>GPL</strong>. Het GPL is een software licentie die ontwikkeld is vanuit het oorspronkelijke principe die gehanteerd werd bij het ontwikkelen van software: de code is vrij en beschikbaar voor iedereen. Dat wil zeggen dat iedereen de achterliggende broncode (de logische stappen die een software programma maakt) kan bekijken en, desgewenst, kan aanpassen of gebruiken in eigen software. Vanaf het moment dat computers, en dus ook besturingssystemen, buiten universiteiten gebruikt werden, kwam er al snel commerciële software op de markt waarvan de broncode niet meer beschikbaar werd gesteld aan het publiek. Dit kan aangezien broncode steeds &#8216;gecompileerd&#8217; wordt - het wordt omgezet naar een taal die de computer kan verstaan. Volgens de beweging achter de GPL, de <strong>Free Software Foundation</strong>, beperkt dit de innovatie en de kennis van software. De GPL is een licentie die stelt dat niet alleen de broncode van het programma beschikbaar moet zijn, maar ook deze van elke aanpassing of derivaat. De GPL is de meest voorkomende opensource (openbron) licentie, andere licenties zijn meestal beperkingen op de GPL zoals de <strong>Lesser GPL</strong> of LGPL. Het model van openbron (opensource) ontwikkeling, waarbij een (vaak grote) groep mensen via interactieve middelen zoals het internet en speciale ontwikkelprogramma&#8217;s aan één project werkt, zoals een software programma of GNU/Linux distributie, heeft in ieder geval al verschillende vruchten geworpen. Het GNU/Linux besturingssysteem is daarvan het voornaamste voorbeeld.</p>
<h3>Hoe ziet GNU/Linux er uit?</h3>
<p>De <strong>grafische omgeving</strong> waarin men werkt, speelt voor veel mensen een belangrijke rol. Vaak worden de systemen OS X en Windows op het grafische vlak vergeleken. Het is nu eenmaal zo, dat de meeste gebruikers veel eisen stellen aan het gebruiksgemak dat met de grafische interface gepaard gaat. Aangezien GNU/Linux een open systeem is, zijn er dan ook verschillende grafische systemen beschikbaar. Opnieuw is de keuze aan de gebruiker en kunnen meerdere grafische systemen apart of samen gebruikt worden. Deze grafische omgevingen variëren van zeer simpele tot zeer complexe systemen, er zijn zelf mogelijkheden die ver voorbij deze van de laatste OS X gaan. Natuurlijk vragen meer mogelijkheden ook betere en dus ook duurdere hardware. Toch is het mogelijk om de meeste grafische omgevingen voor GNU/Linux zo in te stellen dat ze zelfs op oudere computers naar behoren kunnen werken.</p>
<p>Er zijn enkele populaire systemen die een volledige oplossing aanbieden: ze bieden een hele reeks hulpprogramma&#8217;s aan, zoals ondermeer een browser voor zowel bestanden als het internet, een tekstverwerker en allerlei programma&#8217;s voor het bekijken van foto&#8217;s, videos, teksten en het beluisteren van muziek. Deze programma&#8217;s zijn telkens ingewerkt in het systeem zodat ze herkenbaar zijn en een lagere leercurve hebben. Voorbeelden zijn <strong>KDE</strong>, <strong>GNOME</strong> en <strong>XFCE</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://polemian.org/korte-inleiding-gnu-linux/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introduction to XMLSchema Part2 : Datatypes.</title>
		<link>http://polemian.org/introduction-to-xmlschema-part2-datatypes/</link>
		<comments>http://polemian.org/introduction-to-xmlschema-part2-datatypes/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 16:32:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://localhost/~psyched_weels/site2009/?p=44</guid>
		<description><![CDATA[The W3C XML Schema Part 2: Datatypes recommendation states:
“XML Schema: Datatypes is part 2 of the specification of the XML Schema language. It defines facilities for defining datatypes to be used in XML Schema s as well as other XML specifications. The datatype language, which is itself represented in XML 1.0, provides a superset of [...]]]></description>
			<content:encoded><![CDATA[<p>The W3C XML Schema Part 2: Datatypes recommendation states:</p>
<blockquote><p>“<span class="quote">XML Schema: Datatypes is part 2 of the specification of the XML Schema language. It defines facilities for defining datatypes to be used in XML Schema s as well as other XML specifications. The datatype language, which is itself represented in XML 1.0, provides a superset of the capabilities found in XML 1.0 document type definitions (DTDs) for specifying datatypes on elements and attributes.”</span></p></blockquote>
<p><span id="more-44"></span></p>
<p>The XML Schema datatypes definition language is, as it&#8217;s parental language XML, an extensible language. It is implemented in many xml languages or dialects as a way to distinct and validate values. Examples are RDF and Xforms. The following code example uses an XML Schema (xs) datatype to distinct a value used in RDF. The code is taken from the RDF recommendation</p>
<p><strong>Example 1. RDF&#8217;s use of xs datatypes.</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span><span class="re2">?&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;rdf:RDF</span> <span class="re0">xmlns:rdf</span>=<span class="st0">&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">xmlns:ex</span>=<span class="st0">&quot;http://example.org/stuff/1.0/&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;rdf:Description</span> <span class="re0">rdf:about</span>=<span class="st0">&quot;http://example.org/item01&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;ex:size</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">rdf:datatype</span>=<span class="st0">&quot;http://www.w3.org/2001/XMLSchema#int&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 123<span class="sc3"><span class="re1">&lt;/ex:size<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/rdf:Description<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/rdf:RDF<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>Line 5 states that the datatype of the value given for the <em>ex:size</em> of <em>http://example.org/item01</em> (123) is <em>int</em></p>
<p><strong>Example 2. XForms use of xs datatypes.</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> <span class="sc3"><span class="re1">&lt;xforms:model<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;xforms:bind</span> <span class="re0">nodeset</span>=<span class="st0">&quot;/my:payment/my:number&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">relevant</span>=<span class="st0">&quot;/my:payment/@method = &#8216;cc&#8217;&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">required</span>=<span class="st0">&quot;true()&quot;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">type</span>=<span class="st0">&quot;my:ccnumber&quot;</span><span class="re2">/&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;xforms:bind</span> <span class="re0">nodeset</span>=<span class="st0">&quot;/my:payment/my:expiry&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">relevant</span>=<span class="st0">&quot;/my:payment/@method = &#8216;cc&#8217;&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">required</span>=<span class="st0">&quot;true()&quot;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">type</span>=<span class="st0">&quot;xsd:gYearMonth&quot;</span><span class="re2">/&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;xsd:schema<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;xsd:simpleType</span> <span class="re0">name</span>=<span class="st0">&quot;ccnumber&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;xsd:restriction</span> <span class="re0">base</span>=<span class="st0">&quot;xsd:string&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;xsd:pattern</span> <span class="re0">value</span>=<span class="st0">&quot;d{14,18}&quot;</span><span class="re2">/&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/xsd:restriction<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/xsd:simpleType<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/xsd:schema<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/xforms:model<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>This XForms model uses XML Schema datatypes twice: First while using the buil-in datatype <em>gYearMonth</em> to type the node <em>/my:payment/my:expiry</em> and then by deriving a new datatype called <em>ccnumber</em> from the buil-in <em>string</em> datatype. This example is taken from the XForms recommendation</p>
<h3>Type System</h3>
<p>A datatype is (in this recommendation) a 3-tuple consisting of value space, lexical space and a set of characterizing facets.</p>
<p><strong>Figure 1. The 3-tuple : value space, lexical space and facets</strong></p>
<p><img class="alignleft size-full wp-image-65" title="typesystem" src="http://polemian.org/site2009/wp-content/uploads/2009/03/typesystem.png" alt="The 3-tuple : value space, lexical space and facets" width="451" height="122" /></p>
<p>The value space holds the possible values and the lexical space holds the possible lexical representations of those values. If a datatype has more possible lexical representations - eg. like time or dates – the lexical space is referred to as &#8216;canonical&#8217;.</p>
<p>Facets define or restrict the value space of the datatype. The fundamental facets (<em>equal</em>, <em>ordered</em>, <em>bounded</em>, <em>cardinality</em> and <em>numeric</em>) semantically characterize while constraining facets (<em>length</em>, <em>minLength</em>, <em>maxLength</em>, <em>pattern</em>, <em>enumeration</em>, <em>whiteSpace</em>, <em>maxInclusive</em>, <em>maxExclusive</em>, <em>minExclusive</em>, <em>minInclusive</em>, <em>totalDigits</em>, <em>fractionDigits</em>) constrain the value space.</p>
<p>Datatypes can also be categorized by specific dimensions (Characterization dichotomies). The recommendation comes with a list of build-in datatypes, all other datatypes are user-derived (eg. written by you). Some build-in datatypes are primitive; They form the basis from which other datatypes are derived. Eg. A token is derived from normalizedString, which is derived from string. String is a primitive datatype.</p>
<p><strong>Figure 2. Datatype Dichotomies</strong></p>
<p><img class="size-full wp-image-64 alignleft" title="dichotomies" src="http://polemian.org/site2009/wp-content/uploads/2009/03/dichotomies.png" alt="dichotomies" width="341" height="163" /></p>
<p>The most remarkable dichotomy is atomic versus list versus union. Datatypes can be either atomic (they hold only one indivisible value), lists (they hold a – possible empty - list of indivisible values) or unions (they combine the value and lexical space of one or several datatypes).</p>
<p><br style="clear:both;" /><br />
<strong>Figure 3. The build-in datatypes</strong></p>
<p><img class="size-full wp-image-63 alignnone" title="buildintypes" src="http://polemian.org/site2009/wp-content/uploads/2009/03/buildintypes.png" alt="buildintypes" width="560" height="461" /><br />
<br style="clear:both;" /><br />
The recommendation decribes all these datatypes into detail.</p>
<hr /><sup>[<a id="ftn.xsdrec" class="para" href="http://www.polemian.org/style/#xsdrec">1</a>] </sup>XML Schema Part 2: Datatypes Second Edition [<a class="ulink" href="http://www.w3.org/TR/xmlschema-2/" target="_top"><em class="citetitle">http://www.w3.org/TR/xmlschema-2/</em></a>]</p>
<p><sup>[<a id="ftn.id2622519" class="para" href="http://www.polemian.org/style/#id2622519">2</a>] </sup>RDF/XML Syntax Specification (Revised) [<a class="ulink" href="http://www.w3.org/TR/rdf-syntax-grammar/" target="_top"><em class="citetitle">http://www.w3.org/TR/rdf-syntax-grammar/</em></a>]</p>
<p><sup>[<a id="ftn.id2622593" class="para" href="http://www.polemian.org/style/#id2622593">3</a>] </sup>XForms 1.0 (Second Edition) [<a class="ulink" href="http://www.w3.org/TR/xforms/" target="_top"><em class="citetitle">http://www.w3.org/TR/xforms/</em></a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://polemian.org/introduction-to-xmlschema-part2-datatypes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introduction to Mozilla&#8217;s Necko.</title>
		<link>http://polemian.org/introduction-to-mozillas-necko/</link>
		<comments>http://polemian.org/introduction-to-mozillas-necko/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 12:08:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Mozilla & Firefox]]></category>

		<guid isPermaLink="false">http://localhost/~psyched_weels/site2009/?p=21</guid>
		<description><![CDATA[This short example shows how one can download a file from an FTP server using mozilla's Necko networking library(s).]]></description>
			<content:encoded><![CDATA[<p><strong> </strong></p>
<p>This short example shows how one can download a file from an FTP server using mozilla&#8217;s <strong>Necko networking library</strong>(s). It is done by constructing the neccesary XPCOM Components and interfaces. First we need to get the FTP Component:</p>
<p><span id="more-21"></span></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> ftpcomponent = Components.<span class="me1">classes</span><span class="br0">&#91;</span><span class="st0">&quot;@mozilla.org/network/protocol;1?name=ftp&quot;</span><span class="br0">&#93;</span>.<span class="me1">createInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Then we <em>QueryInterface()</em> the <em>nsIProtocolHandler</em>. This interface provides protocol specific enhangements to the basic network functions <em>newURI()</em> and <em>newChannel()</em>. The nsIURI and nsIChannel interfaces returned by these functions are not very different. An nsIURI interface is an very descriptive platform independent way to determine a uniform resource identificator - a comprehensive descriptor for a resource. An nsIChannel interface uses the information of an nsIURI and constructs a connection (= channel) between the client and the remote resource.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">// Get the protocol handler.</span></div>
</li>
<li class="li1">
<div class="de1">ftpcomponent.<span class="me1">QueryInterface</span><span class="br0">&#40;</span>Components.<span class="me1">interfaces</span>.<span class="me1">nsIProtocolHandler</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Create an URI and a Channel.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> nsIURI = ftpcomponent.<span class="me1">newURI</span><span class="br0">&#40;</span>url,<span class="kw2">null</span>,<span class="kw2">null</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">var</span> nsIChannel = ftpcomponent.<span class="me1">newChannel</span><span class="br0">&#40;</span>nsIURI<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Streams are low-level data containers. They can be used to transport, distribute, convert etc&#8230; the data they contain. The nsIChannel <em>open()</em> method creates a synchronised connection to the data and returns an nsIInputStream. If the action should be done asynchronously, use the <em>AsyncOpen()</em> method instead.</p>
<p><strong>nsIInputStreams</strong> are not entirely scriptable, so we need to initialise the stream into a scriptable stream. XPCOM provides an interface called nsIScriptableInputStream which is implemented by the <em>@mozilla.org/scriptableinputstream;1</em> component. The returned stream &#8216;aStream&#8217; is the one we will be using.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> stream = nsIChannel.<span class="kw3">open</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> aStream = Components.<span class="me1">classes</span><span class="br0">&#91;</span><span class="st0">&quot;@mozilla.org/scriptableinputstream;1&quot;</span><span class="br0">&#93;</span>.<span class="me1">createInstance</span><span class="br0">&#40;</span>Components.<span class="me1">interfaces</span>.<span class="me1">nsIScriptableInputStream</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">aStream.<span class="me1">init</span><span class="br0">&#40;</span>stream<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>Now we have to read the contents of the stream into a buffer (eg: a javascript String object).</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> buf = <span class="kw2">new</span> String;</div>
</li>
<li class="li1">
<div class="de1">buf = aStream.<span class="me1">read</span><span class="br0">&#40;</span>aStream.<span class="me1">available</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<blockquote><p>Note<br />
An nsIInputStream has a method called read() which is not scriptable. Therefore we have the use the method read() from the nsIScriptableInputStream. Although these methods look the same there is an important differance. They have a different construction. The nsIInputStream method:</p>
<p><code>PRUint32 read(	charPtr* bufPRUint32 count);<br />
charPtr* buf PRUint32 count ;</code></p>
<p>The nsIScriptableInputStream method:<br />
<code>char* read(	PRUint32 count);<br />
PRUint32 count ;</code></p>
<p>Where the first one takes the buffer as a parameter the second simply returns it.</p></blockquote>
<p>Now we close the stream and return the buffer.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">aStream.<span class="kw3">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">return</span> buf;</div>
</li>
</ol>
</div>
<p>Full code listing:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> getFileFromFTP<span class="br0">&#40;</span>url<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Get the ftp component.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">var</span> ftpcomponent = Components.<span class="me1">classes</span><span class="br0">&#91;</span><span class="st0">&quot;@mozilla.org/network/protocol;1?name=ftp&quot;</span><span class="br0">&#93;</span>.<span class="me1">createInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Get the protocol handler.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; ftpcomponent.<span class="me1">QueryInterface</span><span class="br0">&#40;</span>Components.<span class="me1">interfaces</span>.<span class="me1">nsIProtocolHandler</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Create an URI and a Channel.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw2">var</span> nsIURI = ftpcomponent.<span class="me1">newURI</span><span class="br0">&#40;</span>url,<span class="kw2">null</span>,<span class="kw2">null</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> nsIChannel = ftpcomponent.<span class="me1">newChannel</span><span class="br0">&#40;</span>nsIURI<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Create the stream (using scriptable_stream).</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> stream = nsIChannel.<span class="kw3">open</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw2">var</span> aStream = Components.<span class="me1">classes</span><span class="br0">&#91;</span><span class="st0">&quot;@mozilla.org/scriptableinputstream;1&quot;</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">createInstance</span><span class="br0">&#40;</span>Components.<span class="me1">interfaces</span>.<span class="me1">nsIScriptableInputStream</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; aStream.<span class="me1">init</span><span class="br0">&#40;</span>stream<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Read the stream contents into a buffer.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw2">var</span> buf = <span class="kw2">new</span> String;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; buf = aStream.<span class="me1">read</span><span class="br0">&#40;</span>aStream.<span class="me1">available</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Close the stream.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; aStream.<span class="kw3">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Return the buffer.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> buf;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://polemian.org/introduction-to-mozillas-necko/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Converting decimals to base2 (binary) representations.</title>
		<link>http://polemian.org/converting-decimals-to-base2-binary-representations/</link>
		<comments>http://polemian.org/converting-decimals-to-base2-binary-representations/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 16:56:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://localhost/~psyched_weels/site2009/?p=57</guid>
		<description><![CDATA[The base2 or binary representation uses combinations of the 2 values: <em>0</em> and <em>1</em> to form any number. This is done by using the unique functionalities of the powers of 2 sequence.]]></description>
			<content:encoded><![CDATA[<p>The base2 or binary representation uses combinations of the 2 values: <em>0</em> and <em>1</em> to form any number. This is done by using the unique functionalities of the powers of 2 sequence.<br />
<span id="more-57"></span></p>
<p>Each value in the powers of 2 sequence is unique in that it can not be formed by any combination of the others. For example: There is no way to get 8 using more then one of these numbers because the addition of all numbers lower than 8 (1, 2, 4) is 7.</p>
<p>It is possible to form any number by combining these numbers. But any number can be formed with only one unique combination. For example: the number 7 is formed out of 1, 2 and 4. The number 9 out of 8 and 1.</p>
<p>If we make a sequence of 8 powers of 2 and represent them either as zeros or ones, a zero if we don&#8217;t use the power of 2 in our combination, a one if we do.</p>
<p>The representation of the number 9 would then look like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span> dec2bin<span class="br0">&#40;</span>d<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> b=<span class="st0">&#8221;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span>d&gt;=<span class="nu0">1</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b=<span class="br0">&#40;</span><span class="br0">&#40;</span>d%<span class="nu0">2</span>&gt;=<span class="nu0">1</span><span class="br0">&#41;</span>?<span class="st0">&#8216;1&#8242;</span>:<span class="st0">&#8216;0&#8242;</span><span class="br0">&#41;</span>+b;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d=<span class="br0">&#40;</span>d/<span class="nu0">2</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> b;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span> dec2bin<span class="br0">&#40;</span><span class="nu0">30</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;&nbsp; &nbsp;<span class="co1">// Outputs &#8216;11110&#8242;.</span></div>
</li>
</ol>
</div>
<p>This program takes a decimal <em>d</em> and keeps dividing it by two ( <em>d=(d/2)</em> ) while the resulting number is higher or equal to one ( <em>while(d&gt;=1)</em> ). It determines the remainder of each division and accordingly prepends a zero or one to the return value <em>b</em> ( <em>b=((d%2&gt;=1)?&#8217;1&#8242;:&#8217;0&#8242;)+b</em> ).</p>
<h3>Basic Usage of base 2 sequences.</h3>
<p><em>Anne eats one icecream every day. For the sake of historic recollection she wants to store the flavors used. There are 4 flavors: vanilla, chocolate, strawberry and pistache. How should she store it so she can easily determine which flavor is used in a certain icecream?</em></p>
<p>The answer is simple:</p>
<p>Let&#8217;s give each flavor a unique power of 2 value:</p>
<pre>	vanilla		= 8	(1000)
	chocolate	= 4	(0100)
	strawberry	= 2	(0010)
	pistache	= 1	(0001)</pre>
<p>If Anne&#8217;s icecream has vanilla and strawberry that would make</p>
<pre>	8 + 2 = 10</pre>
<p>In base 2 notation that is:</p>
<pre>	1010</pre>
<p>If we would like to know if the flavor vanilla is used in a particular icecream we need to perform a little math; If a 1 occurs at both the icecream and vanilla sequences at the same spot (eg. first from the left) we place a 1 in a result sequence at the same spot. Any other combination (0 and 1, 0 and 0 or 1 and 0) returns 0. Our resulting line now shows:</p>
<pre>	vanilla		= 1000
	icecream	= 1010
	======================
	result		= 1000</pre>
<p>This result sequence is the same as the vanilla sequence; so vanilla is used in the icecream.</p>
<p>In scripting/programming languages this operation (bitwise AND) is mostly represented with the <em>&amp;</em> (ampersant) symbol.</p>
<p><strong>Example 2. </strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> vanilla&nbsp; &nbsp; &nbsp;= <span class="nu0">8</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> chocolate&nbsp; &nbsp;= <span class="nu0">4</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> strawberry&nbsp; = <span class="nu0">2</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> pistache&nbsp; &nbsp; = <span class="nu0">1</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> icecream;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; icecream = vanilla + strawberry;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>icecream &amp; vanilla<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&#8216;icecream with vanilla&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&#8216;icecream without vanilla&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>In order to count how many flavors are used in an icecream we just test for all the flavors.</p>
<p><strong>Example 3. </strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> icecream = <span class="nu0">1010</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> c = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>icecream &amp; vanilla<span class="br0">&#41;</span> <span class="br0">&#123;</span> c++; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>icecream &amp; chocolate<span class="br0">&#41;</span> <span class="br0">&#123;</span> c++; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>icecream &amp; strawberry<span class="br0">&#41;</span> <span class="br0">&#123;</span> c++; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>icecream &amp; pistache<span class="br0">&#41;</span> <span class="br0">&#123;</span> c++; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Outputs &#8216;Icecream holds 2 flavors&#8217;.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&#8216;Icecream holds &#8216;</span>+c+<span class="st0">&#8216; flavors&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Using base2 notation we can store information about the kind and number of flavors used in an icecream using a combination of only 4 one&#8217;s and/or zero&#8217;s. Comparing this to storing the actual names of the flavors you have to know that every character uses at least 8 zero&#8217;s or one&#8217;s in base2 notation. For the word vanilla that would add up to 7*8 = 56.</p>
]]></content:encoded>
			<wfw:commentRss>http://polemian.org/converting-decimals-to-base2-binary-representations/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
