473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Shift and Logical Operator - for what?

Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming?

I am not sure why i need to use it? I had some samples of c# codes using it.

Can someone share their experiences why someone should use those operators and what type of scenarios?

Any good snippets and example?

Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
8 2400
You can use the XOR operator for encryption (xoring a byte by a value changes the byte, xoring it back by that value changes it back to the original byte). This type of encryption is weak though.

"Chua Wen Ching" wrote:
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming?

I am not sure why i need to use it? I had some samples of c# codes using it.

Can someone share their experiences why someone should use those operators and what type of scenarios?

Any good snippets and example?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2
Shift operators are used in compression/decompression routines, as well as
some math libraries.
XOR, OR, and AND operations are used frequently when driving hardware
libraries, and quite frequently for graphics systems.

These are all more commonly used in memory-constrained systems, which is not
as common as it used to be. That said, with the increased emphasis on
palm-top devices, there's still room for folks who care about using every
byte carefully.

--- Nick

"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming?
I am not sure why i need to use it? I had some samples of c# codes using it.
Can someone share their experiences why someone should use those operators and what type of scenarios?
Any good snippets and example?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #3
Shift operators are most often used in bit-arrays, such as flags. Generally
bit manipulation operators are useful as an efficient replacement for
2-to-the-power operations.
An example is something like:

if (myFlag & (1<<n)==1) ...

Which tests the n'th bit of myFlag. By the book you'd do this as:

if (myFlag & (Math.Pow(2, n+1))==1) ...

Which is far less efficient.

--
John Wood
email: john dot wood at priorganize dot com

"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming?
I am not sure why i need to use it? I had some samples of c# codes using it.
Can someone share their experiences why someone should use those operators and what type of scenarios?
Any good snippets and example?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #4
> I had some beginner questions. Do we need Shift << >> or Logical AND OR
XOR operator in our daily programming?
In the KeyDown event the variable passed to the handler contains an enum
where certain bitflags are set for example wheather additionally to the key
Alt or Ctrl Key were down:

if ((key&Keys.Alt)!=0) { } // tests wheather Alt key were down
Key key = key &~Keys.Alt; // removes alt key from variable and returns pure
pressed key

But you're right, in C# bitmanipulations are used very rarely.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #5
For writing avarage applications these operations are most useful for
packing multiple informations in one variable. Examples are base64
encoding/decoding (see
http://www.thecodeproject.com/csharp/Base64EncDec.asp) or split a TCP/IP
address in network and node address parts. Most the of the hardware devices
exposes the registers with packed information to the software. I.e. in
device drivers you can find lots of logical and shift operations. Logical
and shift operations are also key for encryption/decryption algorithms.

Stefan
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming?
I am not sure why i need to use it? I had some samples of c# codes using it.
Can someone share their experiences why someone should use those operators and what type of scenarios?
Any good snippets and example?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #6
Thanks everyone for the tips.

Normally it is used for advance calculations. :)

Thanks again.
--
Regards,
Chua Wen Ching :)
"Stefan Züger" wrote:
For writing avarage applications these operations are most useful for
packing multiple informations in one variable. Examples are base64
encoding/decoding (see
http://www.thecodeproject.com/csharp/Base64EncDec.asp) or split a TCP/IP
address in network and node address parts. Most the of the hardware devices
exposes the registers with packed information to the software. I.e. in
device drivers you can find lots of logical and shift operations. Logical
and shift operations are also key for encryption/decryption algorithms.

Stefan
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR

XOR operator in our daily programming?

I am not sure why i need to use it? I had some samples of c# codes using

it.

Can someone share their experiences why someone should use those operators

and what type of scenarios?

Any good snippets and example?

Thanks.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #7
I have recently used bitwise & and | in hardware programming. for example
below:

public ASPILayer()

{

UInt32 retVal;

retVal = GetASPI32SupportInfo();
switch(((byte)((retVal & 0xFFFF) >> 8)))

{

case (byte)ErrorCode.SS_COMP:

hostAdaptCount = (byte)((retVal & 0xFFFF) & 0x00FF);

break;

case (byte)ErrorCode.SS_NO_ADAPTERS:

hostAdaptCount = 0;

break;

default:

throw new InitException();

}

}
"ech0" <ec**@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
You can use the XOR operator for encryption (xoring a byte by a value changes the byte, xoring it back by that value changes it back to the
original byte). This type of encryption is weak though.
"Chua Wen Ching" wrote:
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming?
I am not sure why i need to use it? I had some samples of c# codes using it.
Can someone share their experiences why someone should use those operators and what type of scenarios?
Any good snippets and example?

Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #8
xor (^) is commonly used in encryption schemes, since xor'ing a value
against the same number twice gives you the original value. i.e., 255^1
gives you 254, and 254^1 gives you back the original 255. The shift
operators >> and << are also used to pack data and are used in many
encryption or base-conversion (decimal to hexadecimal numbers for instance)
schemes.

An old programmer's trick is to use << and >> to multiply or divide an
integer by a power of 2 (for instance (x >> 1) divides x by 2, and (x << 4)
multiplies x by 16). The shift operators are based on assembly language
instructions that generally operate much faster (fewer clock cycles) than
the equivalent integer multiply and divide instructions. So they can be
useful in other areas besides complex calculations.

Hope that helps,
Michael C.
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:1D**********************************@microsof t.com...
Hi,

I had some beginner questions. Do we need Shift << >> or Logical AND
OR XOR operator in our daily programming?

I am not sure why i need to use it? I had some samples of c# codes
using it.

Can someone share their experiences why someone should use those
operators and what type of scenarios?

Any good snippets and example?

Thanks.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #9

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

Similar topics

4
by: Glen Able | last post by:
Just to get my head straight on this... Firstly, am I right in thinking that right-shifting a signed integer has an undefined result (i.e. could be implemented as a logical or arithmetic shift)?...
80
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
4
by: Kevin | last post by:
I was looking through some source code and noticed the used of the C# << operator. Why is this being used here and under what circumstances is an left-shift operator useful. internal enum...
9
by: yuyang08 | last post by:
Dear all, Is there a logic shift operator in C++? I tried ">>", it is an arithmetic shift operator. Thanks! -Andy
24
by: Nishu | last post by:
Hi All, Could you please explain whether C standard supports logical right shift operation using some operator? I know somewhere I read about >>operator. I thought, it is in C but i think i'm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
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,...
0
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
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...
0
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
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,...

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.