473,480 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

the OR operator

hi, i'm sorry to bother you; but i was wondering what the function of the OR
operator was in terms of numbers:

num0 = num1 or num2

i wrote me a little thing to try and see the effects and if the numbers are
close, they add up; if they're real far apart, it gives the bigest only, and
sometimes it gives the biggest +1 or +2.

could someome please confirm the effects.
thanks in advance
Dan.
Jul 17 '05 #1
7 4012

num0 = num1 or num0 = num2

Dan. wrote:
hi, i'm sorry to bother you; but i was wondering what the function of the OR
operator was in terms of numbers:

num0 = num1 or num2

i wrote me a little thing to try and see the effects and if the numbers are
close, they add up; if they're real far apart, it gives the bigest only, and
sometimes it gives the biggest +1 or +2.

could someome please confirm the effects.
thanks in advance
Dan.

Jul 17 '05 #2
On Sat, 5 Jun 2004 16:26:55 +0200, Dan. wrote:
hi, i'm sorry to bother you; but i was wondering what the function of the OR
operator was in terms of numbers:

num0 = num1 or num2

i wrote me a little thing to try and see the effects and if the numbers are
close, they add up; if they're real far apart, it gives the bigest only, and
sometimes it gives the biggest +1 or +2.

could someome please confirm the effects.
thanks in advance
Dan.


It's a logical operator. For logic tests, it says "if either value is
true, then the test is true".

For numbers, it works at the bit level. If either bit is 1, the result
is 1. The result is only 0 if both bits are 0.

10010110 (decimal 150)
OR 10110101 (decimal 181)
-----------
10110111 (decimal 183)

To play around with the numbers, just open the windows calculator and
switch to scientific mode. On the right side of the display are the
logic buttons. (It's easier to see what's happening if you use binary
numbers (select Bin at the top).)
--
auric underscore underscore at hotmail dot com
*****
Face down, arms out
Nailed to the cross of doubt
Blood runs like rain
Drowning for this world in vain
Crown of black thorns
Human skin, ripped and torn
Where is your savior now?
Where is your savior now?
-- Fear Factory, "Pisschrist"
Jul 17 '05 #3
but what's the criteria for defining which statement executes?

"Lewis Gardner" <lg******@simplifiedtechnologies.com> a écrit dans le
message de news:40**********@news.iglou.com...

num0 = num1 or num0 = num2

Dan. wrote:
hi, i'm sorry to bother you; but i was wondering what the function of the OR operator was in terms of numbers:

num0 = num1 or num2

i wrote me a little thing to try and see the effects and if the numbers are close, they add up; if they're real far apart, it gives the bigest only, and sometimes it gives the biggest +1 or +2.

could someome please confirm the effects.
thanks in advance
Dan.

Jul 17 '05 #4
The results can be different, so OR and + are not truly interchangeable.
Consider:

x=1
y=3
z1 = x Or y
z2 = x + y

? z1, z2
3 4
OR (and AND) are logical operators which in conjunction with grouping
parentheses, can be used to build sophisticated logical expressions. On
numeric values OR performs what is called a "bitwise disjunction" on the two
numeric values. This makes it handy for comparisons such as:

w=5
x=1
y=3

if (x or w) < (y or w) then...

as:

Debug.Print (x Or w) < (y Or w),
Debug.Print (x + w) < (y + w)

Debug.Print (x Or w), (y Or w)
Debug.Print (x + w), (y + w)
True True

5 7
6 8

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Dan." <no@ddress.fr> wrote in message news:c9**********@news.tiscali.fr...
: hi, i'm sorry to bother you; but i was wondering what the function of the
OR
: operator was in terms of numbers:
:
: num0 = num1 or num2
:
: i wrote me a little thing to try and see the effects and if the numbers
are
: close, they add up; if they're real far apart, it gives the bigest only,
and
: sometimes it gives the biggest +1 or +2.
:
: could someome please confirm the effects.
: thanks in advance
: Dan.
:
:

Jul 17 '05 #5
: but what's the criteria for defining which statement executes?

What 'statement' do you mean? Lewis' example or yours?

Your code "num0 = num1 or num2" says "num0 equals the bitwise addition of
the values contained in num1 and num2". It is one expression that is fully
evaluated.

Lewis's code is - a bit more confusing - ... has:

num0 = num1 or num0 = num2

.... In VB this line is actually evaluated as representing the equation:

num0 = num1 Or (num0 = num2)

... which therefore assigns num0 the result of a bitwise addition of num1
with the result of the expression "num0 equals num2" (a true or false
value).

Thus, if:

num0 = 2
num1 = 1
num2 = 3

num0 = num1 Or (num0 = num2)
Print num0
1
whereas if:

num0 = 4
num1 = 2
num2 = 1

num0 = num1 Or (num0 = num2)
Print num0 2


Regardless, all parts of the line (expression) are evaluated .. there is no
'short circuiting' in one-line And/Or statements in VB.
--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Dan." <no@ddress.fr> wrote in message news:c9**********@news.tiscali.fr...

:
: "Lewis Gardner" <lg******@simplifiedtechnologies.com> a icrit dans le
: message de news:40**********@news.iglou.com...
: >
: > num0 = num1 or num0 = num2
: >
: > Dan. wrote:
: > > hi, i'm sorry to bother you; but i was wondering what the function of
: the OR
: > > operator was in terms of numbers:
: > >
: > > num0 = num1 or num2
: > >
: > > i wrote me a little thing to try and see the effects and if the
numbers
: are
: > > close, they add up; if they're real far apart, it gives the bigest
only,
: and
: > > sometimes it gives the biggest +1 or +2.
: > >
: > > could someome please confirm the effects.
: > > thanks in advance
: > > Dan.
: > >
: > >
:
:

Jul 17 '05 #6
"Dan." wrote:

hi, i'm sorry to bother you; but i was wondering what the function of the OR
operator was in terms of numbers:

num0 = num1 or num2


Think of it in terms of the actual bits used by
the computer. Any bit position that has a 1 in
either input number will have a 1 in the output
number:

num1 1011 (11) 0000 (0)
num2 OR 0110 (6) OR 0110 (6)
---- ----
num0 1111 (15) 0110 (6)

AND works the opposite; any bit position that
doesn't have both 1's will output 0:

1011 (11) 1111 (15)
AND 0000 (0) AND 0010 (2)
---- ----
0000 (0) 0010 (2)

--------------------------------------------

'Bitmasking', as it's called, can be useful if
you have a lot of yes/no type data to store
really compactly.

Let's say we have an 8-bit variable CarData;
each bit can represent some feature that is
or is not found on a particular car:

stock/enhanced CD player------
sport package--------------- |
power steering------------ | |
power door locks-------- | | |
convertible----------- | | | |
turbocharger-------- | | | | |
leather seats----- | | | | | |
ABS-------------+ | | | | | | |
0 0 0 0 0 0 0 0

Let's say we have a car with a stock CD player
(its CarData bit 1 is 0), and we want to indicate
that it now has the enhanced player (bit 1 = 1).
BUT, we don't want any other car info to change!

if CarData and 1 = 0 then 'test the value of bit 1
CarData = CarData or 1 'CarData bit 1 is now 1
end if

This is how you set a single bit to zero without
changing any other bits - AND it with a zero bit:

10000101 (133)
AND 11111110 (254)
--------
10000100 (132)

if CarData and 1 = 1 then
CarData= CarData and 254 'only the value of bit 1 changes
end if

Clear as mud? ;-)
Jul 17 '05 #7
yep, i get it now. thanks for your help.

"Randy Day" <ru****@sasktel.nex> a écrit dans le message de
news:40***************@sasktel.nex...
"Dan." wrote:

hi, i'm sorry to bother you; but i was wondering what the function of the OR operator was in terms of numbers:

num0 = num1 or num2


Think of it in terms of the actual bits used by
the computer. Any bit position that has a 1 in
either input number will have a 1 in the output
number:

num1 1011 (11) 0000 (0)
num2 OR 0110 (6) OR 0110 (6)
---- ----
num0 1111 (15) 0110 (6)

AND works the opposite; any bit position that
doesn't have both 1's will output 0:

1011 (11) 1111 (15)
AND 0000 (0) AND 0010 (2)
---- ----
0000 (0) 0010 (2)

--------------------------------------------

'Bitmasking', as it's called, can be useful if
you have a lot of yes/no type data to store
really compactly.

Let's say we have an 8-bit variable CarData;
each bit can represent some feature that is
or is not found on a particular car:

stock/enhanced CD player------
sport package--------------- |
power steering------------ | |
power door locks-------- | | |
convertible----------- | | | |
turbocharger-------- | | | | |
leather seats----- | | | | | |
ABS-------------+ | | | | | | |
0 0 0 0 0 0 0 0

Let's say we have a car with a stock CD player
(its CarData bit 1 is 0), and we want to indicate
that it now has the enhanced player (bit 1 = 1).
BUT, we don't want any other car info to change!

if CarData and 1 = 0 then 'test the value of bit 1
CarData = CarData or 1 'CarData bit 1 is now 1
end if

This is how you set a single bit to zero without
changing any other bits - AND it with a zero bit:

10000101 (133)
AND 11111110 (254)
--------
10000100 (132)

if CarData and 1 = 1 then
CarData= CarData and 254 'only the value of bit 1 changes
end if

Clear as mud? ;-)

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8015
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
3848
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
3783
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
1815
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
2926
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
4398
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
18782
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
2266
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
2470
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
3248
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
7048
marktang
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,...
0
7091
jinu1996
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...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
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...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
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 ...
0
185
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.