/    Sign up×
Community /Pin to ProfileBookmark

How to use the bitwise operator & ?

Hi!

I found a password generator on Interner and it looks like this (my question is in the code):

[code]<?php
// I would like to use lower cased, upper cased and digits, but I cannot set
// the bits, or at least it doesn´t work. What´s wrong?
echo generatePassword(9, 1&2&4);

function generatePassword($length=9, $strength=0) {
$vowels = ‘aeuy’;
$consonants = ‘bdghjmnpqrstvz’;
if ($strength & 1) {
$consonants .= ‘BDGHJLMNPQRSTVWXZ’;
}
if ($strength & 2) {
$vowels .= “AEUY”;
}
if ($strength & 4) {
$consonants .= ‘23456789’;
}
if ($strength & 8) {
$consonants .= ‘@#$%’;
}

$password = ”;
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password .” “.($strength & 1&2&4);
}

?>[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 30.2010 — Just use [b]7[/b] as the second function parameter (4 + 2 + 1 or binary "0111").
Copy linkTweet thisAlerts:
@RoxxorauthorDec 30.2010 — Ah, thanks!
Copy linkTweet thisAlerts:
@tracknutDec 30.2010 — "1&2&4" is and-ing the bits together, and the result is 0. "1|2|4" would or them together, which = 7, which is what you wanted to do.

Dave
×

Success!

Help @Roxxor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...