473,804 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

<< and >> operators in c#

Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

Thanks,
Ben
Jan 21 '07 #1
9 2554
Ben wrote:
I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
I think you have seen too little code.

They are used - both for real bit manipulation
stuff and for multiplication and division with
powers of two.

Most CPU's actually implement them in their
native instruction set.
Arne
Jan 21 '07 #2
They are used - both for real bit manipulation
stuff and for multiplication and division with
powers of two.

Most CPU's actually implement them in their
native instruction set.
In fact that's why we have them.

k = j << 3;

is a lot faster than

k = j * 8;

Jan 22 '07 #3

Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?
it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve

Jan 22 '07 #4
Maybe you would like to expand on the 'piping and 'adding' bits.

Also, what makes you think that bit-shifting is seldom used?
"Steve Richter" <St************ @gmail.comwrote in message
news:11******** *************@m 58g2000cwm.goog legroups.com...
>
Ben wrote:
>Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve

Jan 22 '07 #5

Stephany Young wrote:
Maybe you would like to expand on the 'piping and 'adding' bits.
arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
Also, what makes you think that bit-shifting is seldom used?
I have not used bit shifting for years. If you are looking to multiply
or divide, better to use those operators. ( and the optimized compilers
of today likely bit shift when we multiply by a constant that is a
power of 2. )

-Steve

>

"Steve Richter" <St************ @gmail.comwrote in message
news:11******** *************@m 58g2000cwm.goog legroups.com...

Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?
it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve
Jan 22 '07 #6
arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
And what language might that be defined in?
I have not used bit shifting for years. If you are looking to multiply
Just because you personally have not used a powerful element of the language
for years, don't assume that other people don't use it regularly.
"Steve Richter" <St************ @gmail.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
>
Stephany Young wrote:
>Maybe you would like to expand on the 'piping and 'adding' bits.

arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
>Also, what makes you think that bit-shifting is seldom used?

I have not used bit shifting for years. If you are looking to multiply
or divide, better to use those operators. ( and the optimized compilers
of today likely bit shift when we multiply by a constant that is a
power of 2. )

-Steve

>>

"Steve Richter" <St************ @gmail.comwrote in message
news:11******* **************@ m58g2000cwm.goo glegroups.com.. .
>
Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve

Jan 22 '07 #7

Stephany Young wrote:
arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.

And what language might that be defined in?
a great language. one that also allows the programmer to define the
types that a class can be implicitly converted to. that is a feature
C# could benefit from. I dont like casting.
>
I have not used bit shifting for years. If you are looking to multiply

Just because you personally have not used a powerful element of the language
for years, don't assume that other people don't use it regularly.
what do they use it for? I think it makes a poor bit shift because it
ties you too close to the byte size of what is being operated on and
the unit of measure is implied as bits. why not a syntax that allows
you to shift by bits, bytes, characters?
wordValue = wordValue ShiftLeft 2 Bytes ;

-Steve
>

"Steve Richter" <St************ @gmail.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .

Stephany Young wrote:
Maybe you would like to expand on the 'piping and 'adding' bits.
arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
Also, what makes you think that bit-shifting is seldom used?
I have not used bit shifting for years. If you are looking to multiply
or divide, better to use those operators. ( and the optimized compilers
of today likely bit shift when we multiply by a constant that is a
power of 2. )

-Steve

>

"Steve Richter" <St************ @gmail.comwrote in message
news:11******** *************@m 58g2000cwm.goog legroups.com...

Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve
Jan 22 '07 #8
Hi Steve,

that would be using an operator to change one of it's operands. I think this
is not an OOP way to do it. It surely is not a C#-like way to do. (Though it
would be possible.)
One should use an Add-method instead of that (like in StringBuilder) or the
+ operator like in string, if the type has value semantic.

I suppose the inventors of C# felt this use of the << and >operators an
abuse of them, so they intentionally forbade it.
"Steve Richter" <St************ @gmail.comschri eb im Newsbeitrag
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
>
Stephany Young wrote:
>Maybe you would like to expand on the 'piping and 'adding' bits.

arrayOfLines << new string( "another line" ) ; // adds to the end
of the array
databaseTable << row ; // adds a row to the database table

the general rule being:
<< adds to something
= replaces the contents of something with something else.
>Also, what makes you think that bit-shifting is seldom used?

I have not used bit shifting for years. If you are looking to multiply
or divide, better to use those operators. ( and the optimized compilers
of today likely bit shift when we multiply by a constant that is a
power of 2. )

-Steve

>>

"Steve Richter" <St************ @gmail.comwrote in message
news:11******* **************@ m58g2000cwm.goo glegroups.com.. .
>
Ben wrote:
Hi,

I'm trying to figure out the purpose of these operators.
Now, I know what they do. They shift bits either left or right.
But I don't know why would anybody want to do that. I've never seen
anyone using it in his/her code.
Any ideas?

it is a tragic mistake that an operator that is tailor made for piping
and adding to lists, is reserved instead for such a trivial, seldom
used operation. If you want to shift bits, better to have a new literal
operator:
bunchOfBits shiftleft 2 ;

-Steve

Jan 22 '07 #9
Steve Richter wrote:
And what language might that be defined in?

a great language. one that also allows the programmer to define the
types that a class can be implicitly converted to. that is a feature
C# could benefit from. I dont like casting.
So you find << to be more obvious than list.Add(...) and you don't like
telling readers of your code when a conversion is taking place. I'm
beginning to spot a pattern here...

By the way, you *can* do implicit conversions in C#. There are some
restrictions, and personally I don't like them because they tend to
reduce readability, but they're available.

Jon

Jan 22 '07 #10

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

Similar topics

44
3385
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a tuple OR as the number 5 in a mathematical expression such as x = (5) * (4+6). Wouldn't it have been better to define tuples with <>'s or {}'s or something else to avoid this confusion?? Perhaps ()'s are a good idea for some other reason I don't know?
2
3237
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
2
10572
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script type="text/javascript"> <!]> </script> <script type="text/javascript"
1
1334
by: PengYu.UT | last post by:
I heard that != >= > <= operators are defined based on the operator == <. Could you tell me which header file defines the four operators? Best wishes, Peng
8
1570
by: Greenhorn | last post by:
Hi, Those relational operators have operands of all numerical type int,char,float etc. They also are working for character arrays. Whats the logic behind their working. Is the length of the array compared first and then each character compared with corresponding character. { const char msg = "msessage}", ch = "Za";
1
2044
by: ±èÀçȲ | last post by:
//this code generates the error. uint a=1,b=2; Console.WriteLine(a << b); Console.WriteLine(a >> b); What problem does "uint type" have.?
5
2418
by: Suman | last post by:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++ archives and Stroustrup's FAQs (particularly the following: <url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>) I am left to wondering why Stroustrup doesn't mention the *_cast<> operators or operator .* in that particular FAQ. Archived discussions have convinced me that .* _cannot_ be overloaded. I have gathered a notion (perhaps incorrectly) that the *_cast<>...
3
3386
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above functionality without having to use &gt,&lt and such stuff???
3
2945
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I suppose it's supported and should behave well when I used it correctly. BUT it didn't. Here is my code snippet: class MyKey { public: virtual void foo() { return; }
2
1282
by: defn noob | last post by:
What does >and << do? Googling on them and they are just ignored...
0
9706
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, 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...
0
10571
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, 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...
0
10075
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
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();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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 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...

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.