I'm attempting to do some work around existing code that uses bitwise
operations to manage flags passed into a function and I'm quite
frankly unequipped to do so. I've never done much with bitwise
operations and all the reading I've done today doesn't appear to be
helping me much. I'm hoping someone here can provide a remedial
lesson. Given the following constants:
define('FLAG_1, 1);
define('FLAG_2' , 2);
define('FLAG_3' , 4);
define('FLAG_4' , 8);
define('FLAG_5' , 16);
define('FLAG_6' , 32);
define('FLAG_7' , 64);
define('FLAG_8' , 128);
define('FLAG_9' , 256);
define('FLAG_10 ', 512);
define('FLAG_11 ', 1024);
define('FLAG_12 ', 2048);
I'm making this call:
$foo = my_function ( 'arg1', 'arg2', FLAG_1|FLAG_6|F LAG_4 );
To this function signature:
function my_function ( $arg1, $arg2='blah', $flags=NULL )
Inside the function, if I just echo the value of $args I get "41".
Is that correct? From my reading, it doesn't seem correct, but it may
just be my ignorance in this area. Any assistance reading and
understanding this stuff would be much appreciated.
Thanks.
Rob 10 1772
On Dec 1, 9:30 pm, Rob Wilkerson <r.d.wilker...@ gmail.comwrote:
I'm attempting to do some work around existing code that uses bitwise
operations to manage flags passed into a function and I'm quite
frankly unequipped to do so. I've never done much with bitwise
operations and all the reading I've done today doesn't appear to be
helping me much. I'm hoping someone here can provide a remedial
lesson. Given the following constants:
define('FLAG_1, 1);
define('FLAG_2' , 2);
define('FLAG_3' , 4);
define('FLAG_4' , 8);
define('FLAG_5' , 16);
define('FLAG_6' , 32);
define('FLAG_7' , 64);
define('FLAG_8' , 128);
define('FLAG_9' , 256);
define('FLAG_10 ', 512);
define('FLAG_11 ', 1024);
define('FLAG_12 ', 2048);
I'm making this call:
$foo = my_function ( 'arg1', 'arg2', FLAG_1|FLAG_6|F LAG_4 );
To this function signature:
function my_function ( $arg1, $arg2='blah', $flags=NULL )
Inside the function, if I just echo the value of $args I get "41".
Is that correct? From my reading, it doesn't seem correct, but it may
just be my ignorance in this area. Any assistance reading and
understanding this stuff would be much appreciated.
Thanks.
Rob
FLAG_1 = 1 = 000001
FLAG_6 = 32 = 100000
FLAG_4 = 8 = 001000
If we OR them all together we get 101001 = 41. So that's correct.
Inside your function if you want to check if any particular flag was
set you would do it like this:
if($flags & FLAG_6) {
echo 'FLAG_6 was set';
}
else {
echo 'FLAG_6 was not set';
}
So, in your example, $flags & FLAG_6 would be true because:
$flags = 41 = 101001
FLAG_6 = 32 = 100000
If we AND them together we have 100000 which, inside an if statement,
evaluates to true.
On Dec 1, 9:54 pm, ZeldorBlat <zeldorb...@gma il.comwrote:
>
FLAG_1 = 1 = 000001
FLAG_6 = 32 = 100000
FLAG_4 = 8 = 001000
If we OR them all together we get 101001 = 41. So that's correct.
Inside your function if you want to check if any particular flag was
set you would do it like this:
if($flags & FLAG_6) {
echo 'FLAG_6 was set';}
else {
echo 'FLAG_6 was not set';
}
So, in your example, $flags & FLAG_6 would be true because:
$flags = 41 = 101001
FLAG_6 = 32 = 100000
If we AND them together we have 100000 which, inside an if statement,
evaluates to true.
Interesting. Then I have more learning to do. The function is one
that I'm trying to move into an object oriented context and, given
your validation that the input is correct, what I'm finding is the my
method is doing the right thing for the wrong reasons while the
original function that has always worked is doing the wrong thing for
the right reasons. Enigmatic.
I may have to follow up on this thread, but I have to do my own due
diligence even to know what question to ask.
Thanks for your help.
On Dec 2, 2:54 am, ZeldorBlat <zeldorb...@gma il.comwrote:
On Dec 1, 9:30 pm, Rob Wilkerson <r.d.wilker...@ gmail.comwrote:
I'm attempting to do some work around existing code that uses bitwise
operations to manage flags passed into a function and I'm quite
frankly unequipped to do so. I've never done much with bitwise
operations and all the reading I've done today doesn't appear to be
helping me much. I'm hoping someone here can provide a remedial
lesson. Given the following constants:
define('FLAG_1, 1);
define('FLAG_2' , 2);
define('FLAG_3' , 4);
define('FLAG_4' , 8);
define('FLAG_5' , 16);
define('FLAG_6' , 32);
define('FLAG_7' , 64);
define('FLAG_8' , 128);
define('FLAG_9' , 256);
define('FLAG_10 ', 512);
define('FLAG_11 ', 1024);
define('FLAG_12 ', 2048);
I'm making this call:
$foo = my_function ( 'arg1', 'arg2', FLAG_1|FLAG_6|F LAG_4 );
To this function signature:
function my_function ( $arg1, $arg2='blah', $flags=NULL )
Inside the function, if I just echo the value of $args I get "41".
Is that correct? From my reading, it doesn't seem correct, but it may
just be my ignorance in this area. Any assistance reading and
understanding this stuff would be much appreciated.
Thanks.
Rob
FLAG_1 = 1 = 000001
FLAG_6 = 32 = 100000
FLAG_4 = 8 = 001000
If we OR them all together we get 101001 = 41. So that's correct.
Inside your function if you want to check if any particular flag was
set you would do it like this:
if($flags & FLAG_6) {
echo 'FLAG_6 was set';}
else {
echo 'FLAG_6 was not set';
}
So, in your example, $flags & FLAG_6 would be true because:
$flags = 41 = 101001
FLAG_6 = 32 = 100000
If we AND them together we have 100000 which, inside an if statement,
evaluates to true.
you can think of it like so
"if it is in a and b then put a 1"
so what does
a & b
give you
"if it is in a and b then put a 1"
convert a and b to binary bits if needed
then think of the bits in both,
and run your mental eye down the columns,
put a 1 where the bit is set for both a and b, and a 0 where it isn't
then convert the result into decimal if needed.
17 & 8
10001
01000
so the answer is
10001
01000
00000 (no 1 appears in the any once column)
0 in decimal
how about 7 & 8
0111
1000
again 0
8 & 9
01000
01001
01000
8
with | it's similar
"if it is in a OR b" then put a 1
17 | 8
10001
01000
11001
25
notice this could almost be seen as adding up but it isn't, here's 8
and 9 again:
8 | 9
01000
01001
01001
9
where the two 1 occur twice in the same column we don't end up
doubling it, we just put a 1 in the column if there is a 1 in the
other column in EITHER row.
On Dec 1, 10:15 pm, Rob Wilkerson <r.d.wilker...@ gmail.comwrote:
On Dec 1, 9:54 pm, ZeldorBlat <zeldorb...@gma il.comwrote:
I may have to follow up on this thread, but I have to do my own due
diligence even to know what question to ask.
Okay, I figured out what I was doing wrong and have both functions
behaving exactly the same. Almost. The difference is that when the
function is called, the constants are available to it and the bitwise
expression is passed in as just that - a bitwise expression (is it
properly called an "expression "?). The method, though, is forced to
pass the expression in as a string for evaluation within the method
itself.
So the function gets FLAG_1|FLAG_6|F LAG_4 (evaluating to 41), while
the method gets 'FLAG_1|FLAG_6| FLAG_4' (no evaluation done). In the
method, I split the string, apply the constant values and get the
correct output (1|32|8), but it's still a string. How can I evaluate
that string to get 41?
Thanks again for the help.
Rob Wilkerson wrote:
On Dec 1, 10:15 pm, Rob Wilkerson <r.d.wilker...@ gmail.comwrote:
>On Dec 1, 9:54 pm, ZeldorBlat <zeldorb...@gma il.comwrote:
I may have to follow up on this thread, but I have to do my own due diligence even to know what question to ask.
Okay, I figured out what I was doing wrong and have both functions
behaving exactly the same. Almost. The difference is that when the
function is called, the constants are available to it and the bitwise
expression is passed in as just that - a bitwise expression (is it
properly called an "expression "?). The method, though, is forced to
pass the expression in as a string for evaluation within the method
itself.
So the function gets FLAG_1|FLAG_6|F LAG_4 (evaluating to 41), while
the method gets 'FLAG_1|FLAG_6| FLAG_4' (no evaluation done). In the
method, I split the string, apply the constant values and get the
correct output (1|32|8), but it's still a string. How can I evaluate
that string to get 41?
Thanks again for the help.
Rob,
Your method should be getting the bitwise expression, also. Just
because it's a method in a class doesn't make any difference as to
what's being called.
How about some code?
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. js*******@attgl obal.net
=============== ===
On Dec 1, 10:57 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
Your method should be getting the bitwise expression, also. Just
because it's a method in a class doesn't make any difference as to
what's being called.
How about some code?
The code is all kind of up there. I think maybe I haven't done a very
good job of describing the context so let me try to do better.
In the standalone function solution, the function exists in a file
that is included. That file also includes the constant definitions
(outside the function). When the file containing the constants and
functions is included any code calling the function can use the
constants when passing parameters to the function.
The method, on the other hand, will be part of a class that exists as
a singleton. I'll make the singleton available to other objects (in a
manner I haven't decided yet) and call the method from within that
object as MyClass::getIns tance()->my_method ( ... ). At the time the
call is made, the flags will not be available as constants, but I'd
still like to be able to pass the human readable syntax. To do that,
I've moved the constants into my_method() with the intent of
translating the flag's variable name into its bit value at the top of
the method. That's done and I get the expression I'm expecting (1|32|
8), but not the evaluated value I need. Here's a snippet from the
method that _might_ help:
$result = $myobj->my_method ( 'arg1', 'arg2', 'PREG_FIND_RECU RSIVE|
PREG_FIND_RETUR NASSOC' );
At the top of the function, I have the following code to re-evaluate
the flags into their bits:
$tmp = preg_split ( '/([|&^~])/', $args,
-1 ,PREG_SPLIT_DEL IM_CAPTURE );
$args = '';
foreach ( $tmp as $arg ) {
$args .= defined ( "self::$arg " ) ? constant ( "self::$arg " ) : $arg;
}
At this point, I have '1|32|8' rather than 41. I'm not sure how to
make that next step or, alternatively, to process the string more
effectively as a bit expression.
I hope that helps a little. Thanks again.
Rob Wilkerson wrote:
On Dec 1, 10:57 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>Your method should be getting the bitwise expression, also. Just because it's a method in a class doesn't make any difference as to what's being called.
How about some code?
The code is all kind of up there. I think maybe I haven't done a very
good job of describing the context so let me try to do better.
In the standalone function solution, the function exists in a file
that is included. That file also includes the constant definitions
(outside the function). When the file containing the constants and
functions is included any code calling the function can use the
constants when passing parameters to the function.
The method, on the other hand, will be part of a class that exists as
a singleton. I'll make the singleton available to other objects (in a
manner I haven't decided yet) and call the method from within that
object as MyClass::getIns tance()->my_method ( ... ). At the time the
call is made, the flags will not be available as constants, but I'd
still like to be able to pass the human readable syntax. To do that,
I've moved the constants into my_method() with the intent of
translating the flag's variable name into its bit value at the top of
the method. That's done and I get the expression I'm expecting (1|32|
8), but not the evaluated value I need. Here's a snippet from the
method that _might_ help:
$result = $myobj->my_method ( 'arg1', 'arg2', 'PREG_FIND_RECU RSIVE|
PREG_FIND_RETUR NASSOC' );
At the top of the function, I have the following code to re-evaluate
the flags into their bits:
$tmp = preg_split ( '/([|&^~])/', $args,
-1 ,PREG_SPLIT_DEL IM_CAPTURE );
$args = '';
foreach ( $tmp as $arg ) {
$args .= defined ( "self::$arg " ) ? constant ( "self::$arg " ) : $arg;
}
At this point, I have '1|32|8' rather than 41. I'm not sure how to
make that next step or, alternatively, to process the string more
effectively as a bit expression.
I hope that helps a little. Thanks again.
No, let's see your entire method - and more importantly, the code that's
calling it. You shouldn't need to do any of the stuff you're doing
here. You have something else wrong, but my crystal ball is broken.
So, without the code it's impossible to see what you've got.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. js*******@attgl obal.net
=============== ===
On Dec 1, 11:46 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>
No, let's see your entire method - and more importantly, the code that's
calling it. You shouldn't need to do any of the stuff you're doing
here. You have something else wrong, but my crystal ball is broken.
So, without the code it's impossible to see what you've got.
Okay, then you need a little more background, maybe. I'm working to
adapt someone else's custom function into an object oriented
architecture. The original function (and definition of constants) can
be found at http://www.pgregg.com/projects/php/p...preg_find.phps.
My object oriented translation requires just a few changes to the
function itself and they're really just syntax changes - except for
translating the bitwise expression.
CALLING THE METHOD:
require_once ( 'org/client/file/finder.php' );
$finder = new Finder();
$result = $finder->preg_find ( '/image\.php$/', '/path/to/my/includes/
org/client/content', 'PREG_FIND_RECU RSIVE|PREG_FIND _RETURNASSOC' );
new PHPDump ( $result );
exit();
Note that the flag definitions do not exist yet. If the single quotes
in the third argument are removed then the normal (and expected)
"constant not defined" errors are thrown. Nor, for that matter, do I
*want* them to exist here. I want to keep them with the code where
they matter rather than defining them everywhere they're used.
Similarly, I want to continue to be able to use the flags because
they're more readable than passing in the bit values themselves.
METHOD CHANGES:
class Finder {
const PREG_FIND_RECUR SIVE = 1;
const PREG_FIND_DIRMA TCH = 2;
const PREG_FIND_FULLP ATH = 4;
const PREG_FIND_NEGAT E = 8;
/**
* All of the additional flag constants defined similarly
*
* SNIP -->
*/
public function preg_find ( $pattern, $start_dir='.', $args=NULL ) {
/**
* Go through the business of evaluating the bit string.
*/
$args_in = $args; /** Save off the input string */
$tmp = preg_split ( '/([|&^~])/', $args,
-1 ,PREG_SPLIT_DEL IM_CAPTURE );
$args = '';
foreach ( $tmp as $arg ) {
$args .= defined ( "self::$arg " ) ? constant ( "self::$arg " ) :
$arg;
}
/**
* Back to doing what we need to do using the bitwise expression
that is expected.
*/
static $depth = -1;
++$depth;
/**
* SNIP -->
*/
}
}
Aside from the code shown at the top of the method, only the following
changes from the original were made:
- The syntax of all references to a flag within the method was changed
to "self::FLAG_NAM E".
- In the recursive call to the method, the arguments being passed are
the original arguments saved off at the top of the method.
I've found no other changes that are required thus far.
Once again, I appreciate your help.
Rob Wilkerson wrote:
On Dec 1, 11:46 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>No, let's see your entire method - and more importantly, the code that's calling it. You shouldn't need to do any of the stuff you're doing here. You have something else wrong, but my crystal ball is broken. So, without the code it's impossible to see what you've got.
Okay, then you need a little more background, maybe. I'm working to
adapt someone else's custom function into an object oriented
architecture. The original function (and definition of constants) can
be found at http://www.pgregg.com/projects/php/p...preg_find.phps.
My object oriented translation requires just a few changes to the
function itself and they're really just syntax changes - except for
translating the bitwise expression.
CALLING THE METHOD:
require_once ( 'org/client/file/finder.php' );
$finder = new Finder();
$result = $finder->preg_find ( '/image\.php$/', '/path/to/my/includes/
org/client/content', 'PREG_FIND_RECU RSIVE|PREG_FIND _RETURNASSOC' );
new PHPDump ( $result );
exit();
Note that the flag definitions do not exist yet. If the single quotes
in the third argument are removed then the normal (and expected)
"constant not defined" errors are thrown. Nor, for that matter, do I
*want* them to exist here. I want to keep them with the code where
they matter rather than defining them everywhere they're used.
Similarly, I want to continue to be able to use the flags because
they're more readable than passing in the bit values themselves.
METHOD CHANGES:
class Finder {
const PREG_FIND_RECUR SIVE = 1;
const PREG_FIND_DIRMA TCH = 2;
const PREG_FIND_FULLP ATH = 4;
const PREG_FIND_NEGAT E = 8;
/**
* All of the additional flag constants defined similarly
*
* SNIP -->
*/
public function preg_find ( $pattern, $start_dir='.', $args=NULL ) {
/**
* Go through the business of evaluating the bit string.
*/
$args_in = $args; /** Save off the input string */
$tmp = preg_split ( '/([|&^~])/', $args,
-1 ,PREG_SPLIT_DEL IM_CAPTURE );
$args = '';
foreach ( $tmp as $arg ) {
$args .= defined ( "self::$arg " ) ? constant ( "self::$arg " ) :
$arg;
}
/**
* Back to doing what we need to do using the bitwise expression
that is expected.
*/
static $depth = -1;
++$depth;
/**
* SNIP -->
*/
}
}
Aside from the code shown at the top of the method, only the following
changes from the original were made:
- The syntax of all references to a flag within the method was changed
to "self::FLAG_NAM E".
- In the recursive call to the method, the arguments being passed are
the original arguments saved off at the top of the method.
I've found no other changes that are required thus far.
Once again, I appreciate your help.
The flags do exist in your code, because you've defined them. Your
problem is in how you're using them.
You defined the constants in the class, so you need to scope them to the
class, i.e. Finder::PREG_FI ND_RECURSIVE.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. js*******@attgl obal.net
=============== === This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Randell D. |
last post by:
Why would one use bitwise operators? I can program in various languages in
some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise
operators before, but never understood why anyone would use them - any real
world examples or ideas? Examples follow (that I am reading in my Core
JavaScript Guide 1.5).
15 & 9 yields 9 (1111 & 1001 = 1001)
15 | 9 yields 15 (1111 | 1001 = 1111)
15 ^ 9 yields 6 (1111 ^ 1001 = 0110)
in...
|
by: baustin75 |
last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie
only when debugging in php designer 2005
--------------------------------------------------------------------------------
Hello,
I have a very simple problem but cannot seem to figure it out. I have a
very simple php script that sends a test email to myself. When I debug
it in PHP designer, it works with no problems, I get the test email. If
|
by: jas_lx |
last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.
Having done some Win32 programming in straight C, the Win32 API sends
windows messages to applications with various settings stored in bit
fields, in which case changing the settings of the message is a matter
of using the bitwise operators. This was a good practical example of...
|
by: Paul E Collins |
last post by:
Suppose I have a few Keys objects:
Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J
I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.
|
by: Emilio |
last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
| |
by: rguarnieri |
last post by:
Hi!
I'm trying to create a query with a boolean expression like this:
select (4 and 1) as Value from Table1
this query return always -1, but when I make the same calculation in
visual basic, the value returned is 0.
Can anyone tell me why the expression (4 and 1) return different value
|
by: Carramba |
last post by:
Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can multiple it by 1000 to
get 11111 and the preform bitwise like <<2 to get 88888 and then divide
by 1000 to go back to float 8.8888. but these seem like "nasty" way to
do it. So maybe some of you have great tips?
Thank you in advance!
L R
|
by: Carl Banks |
last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this
is just talk.)
Not many people are bit-fiddling these days. One of the main uses of bit
fields is flags, but that's not often done in Python because of keyword
arguments and dicts, which are lot more versatile. Another major use,
talking to hardware, is not something oft done in Python either.
It seems like this occasional usage wouldn't justify having built-in...
|
by: Daniel Gutson |
last post by:
Hi,
I just wanted to share another library for doing type-safe bitwise
operations in C++:
http://bitwise-enum.googlecode.com
I found it useful, so hopefully it'll be for somebody else as well.
BRgds,
Daniel.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |