Page 1 of 1 1
Topic Options
#18250 - 2002-03-04 11:57 PM Bitwise Mathematical AND operation
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
someone want to give a dissertation on the logic of the & operation (sample code from a script taken for example)

code:

if $productsuite
if $productsuite & 1
$os_suite=$os_suite+'Small Business |'
endif
if $productsuite & 2
$os_suite=$os_suite+'Enterprise |'
endif
if $productsuite & 4
$os_suite=$os_suite+'BackOffice |'
endif
if $productsuite & 8
$os_suite=$os_suite+'Communication Server |'
endif
if $productsuite & 16
$os_suite=$os_suite+'Terminal Server |'
endif
if $productsuite & 32
$os_suite=$os_suite+'Small Business (Restricted) |'
endif
if $productsuite & 64
$os_suite=$os_suite+'Embedded NT |'
endif
if $productsuite & 128
$os_suite=$os_suite+'DataCenter |'
endif
if $productsuite & 256
$os_suite=$os_suite+'Single User Terminal Server |'
endif
if $productsuite & 512
$os_suite=$os_suite+'Home Edition |'
endif
if $productsuite & 1024
$os_suite=$os_suite+'Blade Server |'
endif
if instr($os_suite,'|')
$os_suite=left($os_suite,len($os_suite)-2)
endif

[ 04 March 2002: Message edited by: Radimus ]

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#18251 - 2002-03-05 08:15 AM Re: Bitwise Mathematical AND operation
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Rad,

Rather than expanding on what has been written on this, there is an interesting bit on MSDN.

HTH,

- Kent

_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#18252 - 2002-03-05 03:32 PM Re: Bitwise Mathematical AND operation
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
I understand how to detect versions of the OS.

I want to know about the & operations

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#18253 - 2002-03-05 03:54 PM Re: Bitwise Mathematical AND operation
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Wow! Big subject! [Eek!]

You know that a number can be represented in binary form, and each binary digit ("bit") can be 1 or 0. Well, these can be used as simple yes/no flags.

For instance:
1 = 0001
2 = 0010
3 = 0011
...
15= 1111

You can assign a meaning to each bit.
1 = 0001 = yellow
2 = 0010 = blue
4 = 0100 = car
8 = 1000 = train

To test if one or more bits are set you use a boolean AND (&). For example:
code:
$Transport=5
If $Transport & 1 "Vehicle is YELLOW" ? Endif
If $Transport & 2 "Vehicle is BLUE" ? Endif
If $Transport & 4 "Vehicle is a CAR" ? Endif
If $Transport & 8 "Vehicle is a TRAIN" ? Endif

Will give you:
quote:
Vehicle is YELLOW
Vehicle is a CAR

You may also combine the tests:
code:
If ($Transport & 3)=3 "Vehicle is Green!" EndIf

If you need more explanation try searcing the net for "boolean", "truth tables" and "logic gates".

EDIT: Glad I spotted that error before anyone else...

[ 05 March 2002, 18:14: Message edited by: Richard Howarth ]

Top
#18254 - 2002-03-05 04:01 PM Re: Bitwise Mathematical AND operation
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I'm not quiet sure what exactly you are looking for but I'll give it a try.

The '&' and '|' perform bitwise mathematical operations, anmely AND and OR.

First of all, a number can be represented by a combination of '0' and '1'. Each bit represents the power of two at the bit's position. So, I can write the number 5 as 0*2^3+1*2^2+0*2^1+1*1^0 or binary '0101', a 4-bit number. You then have 8 bits for a byte and 16 bits for a double.

Now, if I am doing a bitwise AND operation then I want to check whether all bits that are set in the first number are also set in the second number.

So, if I have the numbers '5' and '2' and want to do a bitwise AND as in '5 & 2' I can write it as:
5 & 2
or
0101 & 0010
or

0101
&
0010
=
0000
=
0
Therefore, '5 & 2 = 0', and if it is being used in an IF clause, the condition would result in 'FALSE'

On the other hand, '5&4' would be

5&4
or
0101 & 0100
or
0101
&
0100
=
0100
=4

Therefore '5 & 4 = 4', and if it is being used in an IF clause, the condition would result in 'TRUE' since the reuslt is not equal '0'

Finally, if the '|' or OR operator is used, then evry bit is set that is set in either number, e.g. '5 | 2' would be

5 | 2
or
0101
|
0010
=
0111
=
7

So, the OR will always result in TRUE unless you do a '0 | 0' which will always be '0'.

I hope this was what you were looking for.

In the example you provided, the number that is the result of @PRODUCTSUITE is evaluated against all bits from 0-10 with bit 0 equal '1' and bit 10 equal '1024'. So, if I have @PRODUCTSUITE=49, then the bits 1, 16, and 32 are set since 1+16+32=49. Therefore the IF conditions testing @PRODUCTSUITE&1, @PRODUCTSUITE&16, and @PRODUCTSUITE&32 will evaluate to values not equal 0 (thus true)

_________________________
There are two types of vessels, submarines and targets.

Top
#18255 - 2002-03-05 06:13 PM Re: Bitwise Mathematical AND operation
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
So what it is doing is not checking the value, but instead checking the values of specific bits in a binary value...

very interesting....
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#18256 - 2002-03-05 08:38 PM Re: Bitwise Mathematical AND operation
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I guess that's why they call it a BINARY AND [Wink]
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.065 seconds in which 0.032 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org