Article View: pl.comp.lang.php
Article #219942Re: Operatory bitowe - little confusion
From: Lemat
Date: Thu, 30 Jul 2009 19:19
Date: Thu, 30 Jul 2009 19:19
67 lines
2252 bytes
2252 bytes
hazet wrote:
> Nie mogę, zrozumieć jak działają operatory bitowe i kiedy przyjmują TRUE
> Przykładowy kod:
>
> <?php
> define('SILNIK', 1);
> define('KOLA', 2);
> define('SWIATLA', 4);
> define('SKRZYNIA', 8);
> define('PALIWO', 16);
>
> // Raportowanie stanu pojazdu
> function stan($stan)
> {
> if($stan & SILNIK)
> {
> echo 'Silnik jest sprawny<br/>';
> }
> if($stan & KOLA)
> {
> echo 'Koła są sprawne<br/>';
> }
> if($stan & SWIATLA)
> {
> echo 'Światła są sprawne<br/>';
> }
> if($stan & SKRZYNIA)
> {
> echo 'Skrzynia jest sprawna<br/>';
> }
> if($stan & PALIWO)
> {
> echo 'Paliwo jest w baku<br/>';
> }
> } // end stan();
>
>
> stan(SILNIK | SKRZYNIA | PALIWO);
> ?>
>
>
> Są sobie stałe z przypisanymi wartościami (nie działa tak jak powinno
> gdy napisze 1,2,3,4 .. muszą być potęgi 2).
prawidłowo
1 = 00001
2 = 00010
4 = 00100
8 = 01000
16 = 10000
> Kiedy if($stan & PALIWO) jest true ? co jest pod $stan ?
>
> stan(SILNIK | SKRZYNIA | PALIWO); - dlaczego tutaj znalazł się operator
> lub ?
SILNIK | SKRZYNIA | PALIWO = 11001
SILNIK & SKRZYNIA & PALIWO = 00000
(SILNIK | SKRZYNIA | PALIWO) & SILNIK = 10000 (true)
(SILNIK | SKRZYNIA | PALIWO) & SKRZYNIA = 01000 (true)
(SILNIK | SKRZYNIA | PALIWO) & SWIATLA = 00000 (false)
--
Pozdrawiam
Lemat
Technical Details
Message-ID:
<h4srrg$f93$1@atlantis.news.neostrada.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!newsfeed.neostrada.pl!atlantis.news.neostrada.pl!news.neostrada.pl!not-for-mailReferences:
<h4sqk2$huj$1@inews.gazeta.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!newsfeed.neostrada.pl!atlantis.news.neostrada.pl!news.neostrada.pl!not-for-mail From: Lemat <#@lemat.priv.pl> Newsgroups: pl.comp.lang.php Subject: Re: Operatory bitowe - little confusion Followup-To: pl.comp.lang.php Date: Thu, 30 Jul 2009 21:19:22 +0200 Organization: lemat.priv.pl Lines: 69 Message-ID: <h4srrg$f93$1@atlantis.news.neostrada.pl> References: <h4sqk2$huj$1@inews.gazeta.pl> NNTP-Posting-Host: quad.lemat.priv.pl Mime-Version: 1.0 X-Trace: atlantis.news.neostrada.pl 1248981680 15651 79.190.106.116 (30 Jul 2009 19:21:20 GMT) X-Complaints-To: usenet@news.neostrada.pl NNTP-Posting-Date: Thu, 30 Jul 2009 19:21:20 +0000 (UTC) User-Agent: KNode/0.99.01 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit