🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.php
Article #219941

Operatory bitowe - little confusion

#219941
From: hazet
Date: Thu, 30 Jul 2009 19:00
47 lines
1857 bytes
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).

Kiedy if($stan & PALIWO) jest true ? co jest pod $stan ?

stan(SILNIK | SKRZYNIA | PALIWO); - dlaczego tutaj znalazł się operator 
lub ?
Technical Details
Message-ID: <h4sqk2$huj$1@inews.gazeta.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail
From: hazet <hazet@no-mail.pl>
Newsgroups: pl.comp.lang.php
Subject: Operatory bitowe - little confusion
Date: Thu, 30 Jul 2009 21:00:15 +0200
Organization: "Portal Gazeta.pl -> http://www.gazeta.pl"
Lines: 47
Message-ID: <h4sqk2$huj$1@inews.gazeta.pl>
NNTP-Posting-Host: static-ip-77-89-75-190.promax.media.pl
Mime-Version: 1.0
X-Trace: inews.gazeta.pl 1248980418 18387 77.89.75.190 (30 Jul 2009 19:00:18 GMT)
X-Complaints-To: usenet@agora.pl
NNTP-Posting-Date: Thu, 30 Jul 2009 19:00:18 +0000 (UTC)
X-User: vitar91
X-Antivirus: avast! (VPS 090729-1, 2009-07-29), Outbound message
X-Antivirus-Status: Clean
User-Agent: Thunderbird 2.0.0.22 (Windows/20090605)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit