Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Are Bitwise Operators Still Relevant in Modern PHP?
Aug 22, 2017 @ 16:16:47

In a post to the SitePoint PHP site editor Bruno Skvorc wonders if bitwise operators are still relevant in modern PHP development.

Many of you probably scratched your heads reading this title. “Bitwhat?”

In this article, we’ll look at what bitwise operators are, and whether or not their use is still relevant in this modern age of computing.

He starts off by illustrating a common use case for the bitwise operators in evaluating user permissions. He first proposes doing things on the database side, creating tables for double or single joins that could get us the information we need. He also shows an approach for what he calls a "column stampede": adding a new column to the user table when a new permission is needed. Instead he proposes the bitwise option, first explaining how values are stored and then showing how with a single value, you could potentially store all of a user's permissions in one field. Next he shows how to perform the select to determine of a user has a set of permissions and how to store them when making an insert/update.

tagged: bitwise operator modern development permission calculation tutorial

Link: https://www.sitepoint.com/bitwise-operators-still-relevant-modern-php/

Stovepipe Systems:
Using bitwise instead of booleans
Aug 18, 2016 @ 16:18:44

On the Stovepipe Systems blog there's a new post from Yannick de Lange that suggests using bitwose operations instead of booleans to evaluate an on/off setting.

The naive way of storing many boolean options (in a database) is to create for each option a field and storing a 0 when it is false and 1 when it is true. Which of course works, but adding options will require a new field, which might require creating a compatibility layer for your old data. There is an easier way to do this and it's even more efficient at checking fields.

This brings me to an old topic which I have to explain to all the new people at some point and even once explained not everybody understands how it actually works. So in this post I'm going to explain how to use bitwise operators and how it works internally.

He starts with a common example using the 0/1 storage method and refactors it a bit to use different values that are more compatible with bitwise operations. He includes the usage of this system and how to works to evaluate multiple potential option values.

tagged: bitwise option boolean storage enable setting tutorial

Link: http://stovepipe.systems/post/using-bitwise-instead-of-booleans

PHPMaster.com:
Base Converting and Binary Operators
Apr 18, 2013 @ 16:44:05

On PHPMaster.com there's a new tutorial from Timothy Boronczyk focusing on the binary operators in PHP and using them to do some base conversions.

This article discusses PHP’s binary operators and how to convert between different counting systems. Most programming books and articles only dedicate a page or two to such topics, and although using the operators is really quite simple, there’s a fair amount of background knowledge one must have to use them correctly. Instead of giving the same bare-bones treatment that every other reference gives, I’ll first provide you the necessary background in number theory. Armed with that knowledge, you’ll be able to understand the binary counting system and base conversions… and familiarity with binary digits is the key to successful use of the binary operators!

As promised he starts off with some number theory complete with illustrations about base-10 and how the values fit into "buckets" of data. He compares this to base-2 (binary) and only then starts getting into the conversion functions. He shows the usage of things like decbin and dechex to modify the values as well as the use of bitwise and binary operators.

tagged: converting binary bitwise operator tutorial numbertheory

Link: http://phpmaster.com/base-converting-and-binary-operators

Igor Wiedler:
Binary parsing with PHP
Sep 25, 2012 @ 17:17:41

Igor Wiedler has a new post to his blog showing how to work with binary data in your PHP applications a few different built-in functions including unpack and bindec.

Binary operations in PHP are a bit strange. Since PHP was originally a templating layer for C code, it still has many of those C-isms. Lots of the function names map directly to C-level APIs, even if they work a bit differently sometimes. For example, PHP's strlen maps directly to STRLEN(3), and there are countless examples of this. However, as soon as it comes to dealing with binary data, things suddenly become very different.

He starts off looking at "the C way" to unpack a string (getting the ASCII values of each character) and shows how *not* to do it in PHP with ord. Instead he uses "unpack", bitwise operators and bindec to work with the actual binary data of the string.

tagged: binary parse ord unpack tutorial bindec bitwise

Link:

Reddit.com:
How and When do you use Bitwise?
Apr 06, 2012 @ 15:30:32

In this new question to Reddit, there's a question about the usage of bitwise operators - when they might come in useful.

As I get deeper into PHP I notice some lower level functionality that looks like it could be interesting, maybe even useful. I'm trying (other than switching the hex value of a color) to figure out when Bitwise operators might come in handy. Any real world examples?

Responses to the post mention a few cases they'd be useful in - handling permissions, route matching and error reporting. You can find out more about bitwise operators in PHP the PHP manual.

tagged: bitwise usage opinion operator

Link:

Stoimen Popov's Blog:
PHP Performance: Bitwise Division
Jan 06, 2012 @ 15:26:38

Stoimen Popov has a new post to his blog today comparing the performance of bitwise division versus the normal "/" division operator.

Recently I wrote about binary search and then I said that in some languages, like PHP, bitwise division by two is not faster than the typical "/" operator. However I decided to make some experiments and here are the results.

According to his results using the bitwise method is slightly faster, but you'd only really notice it when working with large sets of data (like his example using 10,000,000). The code to run his benchmarks is included in the post.

tagged: bitwise division benchmark compare operator

Link:


Trending Topics: