473,395 Members | 1,999 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,395 software developers and data experts.

Extending operator + (plus) for byte/short type

While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when
i know that the entity counted isn't larger
than 255 (or 65k). However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I'd like to
redeclare/overload the arithmetical
operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

Nov 9 '08 #1
11 1763
K Viltersten wrote:
While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn't larger
than 255 (or 65k). However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I'd like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?
I don't think it is doable.

You should change your coding style and use int. Not only is the space
saving insignificant, but you potentially could end up spending more
CPU cycles and memory for code.

Arne
Nov 9 '08 #2
K Viltersten wrote:
While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn't larger
than 255 (or 65k). However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }
It *should* be a pain. You get a good indication of the work the CPU has to do.

Operating on 32-bit integers is vastly more efficient for a 32-bit CPU than
operating on bytes. The only reason you could have to use bytes is to save
memory, but you have to be pretty tight on that before you can justify the
slowdown in processing. And you have to show that you're actually *saving*
memory: more instructions are needed to manipulate bytes, and those take up
memory too.

About the only time you encounter bytes is in byte arrays. Individual bytes
are converted to ints for any nontrivial operation, and they should stay
that way until you store them back.

Public properties should basically never be smaller than an int, it doesn't
pay (unless, again, you can show that you really need the memory) or when
they're part of a predefined interop structure.

Using "short" or "byte" to enforce ranges is a bad idea: it's easy to throw
in a cast to force the value to be in range, but does this actually produce
the correct result? Checking the range explicitly in the setter is a better
idea.
How can i avoid doing that? I'd like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).
Can't be done in C#. In fact, it can't be done in any .NET language I'm
aware of. Those operations map to CLR primitives and overloading them from a
language would be decidedly strange -- not to mention inefficient.

--
J.
Nov 9 '08 #3
Arne Vajhøj wrote:
K Viltersten wrote:
>While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn't larger
than 255 (or 65k).
That is, 127 or 32767. Both types are signed.
>However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I'd like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?

I don't think it is doable.

You should change your coding style and use int. Not only is the
space
saving insignificant, but you potentially could end up spending more
CPU cycles and memory for code.
Unless you've got arrays of them, I suspect that there's no space
saved by using "byte" or "short" insead of "int". They're probably
allocated on 32-bit boundaries, just like the larger types, with the
unused 24 or 16 bits simply wasted.
Nov 9 '08 #4
Mike Schilling wrote:
Arne Vajhøj wrote:
>K Viltersten wrote:
>>While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the
entity
counted isn't larger
than 255 (or 65k).

That is, 127 or 32767. Both types are signed.
>>However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I'd like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?

I don't think it is doable.

You should change your coding style and use int. Not only is the
space
saving insignificant, but you potentially could end up spending
more
CPU cycles and memory for code.

Unless you've got arrays of them, I suspect that there's no space
saved by using "byte" or "short" insead of "int". They're probably
allocated on 32-bit boundaries, just like the larger types, with the
unused 24 or 16 bits simply wasted.
Please ignore the foregoing; I hadn't realized I was in a C# group
rather than a Java group.
Nov 9 '08 #5
Mike Schilling wrote:
Arne Vajhøj wrote:
>K Viltersten wrote:
>>While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn't larger
than 255 (or 65k).

That is, 127 or 32767. Both types are signed.
Byte is 0..255 because it is unsigned in C# - they got it right.

Short is -32768..32767, but I don't think it is important.
>>However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I'd like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?
I don't think it is doable.

You should change your coding style and use int. Not only is the
space
saving insignificant, but you potentially could end up spending more
CPU cycles and memory for code.

Unless you've got arrays of them, I suspect that there's no space
saved by using "byte" or "short" insead of "int". They're probably
allocated on 32-bit boundaries, just like the larger types, with the
unused 24 or 16 bits simply wasted.
Could very well be.

Arne
Nov 9 '08 #6
Mike Schilling wrote:
Mike Schilling wrote:
>Arne Vajhøj wrote:
>>K Viltersten wrote:
While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the
entity
counted isn't larger
than 255 (or 65k).
That is, 127 or 32767. Both types are signed.
>>>However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }

How can i avoid doing that? I'd like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).

Is it easily doable? Is it doable?
I don't think it is doable.

You should change your coding style and use int. Not only is the
space
saving insignificant, but you potentially could end up spending
more
CPU cycles and memory for code.
Unless you've got arrays of them, I suspect that there's no space
saved by using "byte" or "short" insead of "int". They're probably
allocated on 32-bit boundaries, just like the larger types, with the
unused 24 or 16 bits simply wasted.

Please ignore the foregoing; I hadn't realized I was in a C# group
rather than a Java group.
Besides the byte type range, then it should be just as valid for C#.

Arne
Nov 9 '08 #7
Jeroen Mostert wrote:
K Viltersten wrote:
>While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn't larger
than 255 (or 65k). However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }
It *should* be a pain. You get a good indication of the work the CPU has
to do.

Operating on 32-bit integers is vastly more efficient for a 32-bit CPU
than operating on bytes.
Usually X bit CPU means that the virtual addresses are X bits wide.

It does not say anything about register width, memory bus width, width
of instruction operands etc..

I am rather confident that modern CPU's are indeed more optimized for
operations on 32 bit entities than 8 bit entities (even though x86 is
traditionally very byte and short friendly), so I agree with your
conclusion, but I will not tie it the 32 bitness.
About the only time you encounter bytes is in byte arrays. Individual
bytes are converted to ints for any nontrivial operation, and they
should stay that way until you store them back.
Agree.
Public properties should basically never be smaller than an int, it
doesn't pay (unless, again, you can show that you really need the
memory) or when they're part of a predefined interop structure.
I would tend to let the modeling determine the type.

Arne
Nov 9 '08 #8
Arne Vajhøj wrote:
Jeroen Mostert wrote:
>K Viltersten wrote:
>>While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn't larger
than 255 (or 65k). However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }
It *should* be a pain. You get a good indication of the work the CPU
has to do.

Operating on 32-bit integers is vastly more efficient for a 32-bit CPU
than operating on bytes.

Usually X bit CPU means that the virtual addresses are X bits wide.

It does not say anything about register width, memory bus width, width
of instruction operands etc..
True, true -- although the term "X-bit CPU" is actually vague enough to
indicate any of these things.
>Public properties should basically never be smaller than an int, it
doesn't pay (unless, again, you can show that you really need the
memory) or when they're part of a predefined interop structure.

I would tend to let the modeling determine the type.
If your model actually includes 16-bit and 8-bit types, sure. Most models
don't happen to include things like "integers with a range of -32,768 to
32,767", though -- and when they do, modeling them with the most convenient
integer type (rather than the one that would fit the range exactly) is still
not a bad idea.

--
J.
Nov 9 '08 #9
Jeroen Mostert wrote:
<snip>
If your model actually includes 16-bit and 8-bit types, sure. Most
models don't happen to include things like "integers with a range of
-32,768 to 32,767", though -- and when they do, modeling them with the
most convenient integer type (rather than the one that would fit the
range exactly) is still not a bad idea.
This should be "implementing them", obviously.

--
J.
Nov 9 '08 #10
Arne Vajhøj wrote:
Mike Schilling wrote:
>Mike Schilling wrote:
>>Arne Vajhøj wrote:
K Viltersten wrote:
While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the
entity
counted isn't larger
than 255 (or 65k).
That is, 127 or 32767. Both types are signed.

However, it's a real pain
to cast every time i perform an operation.
>
Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }
>
How can i avoid doing that? I'd like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).
>
Is it easily doable? Is it doable?
I don't think it is doable.

You should change your coding style and use int. Not only is the
space
saving insignificant, but you potentially could end up spending
more
CPU cycles and memory for code.
Unless you've got arrays of them, I suspect that there's no space
saved by using "byte" or "short" insead of "int". They're
probably
allocated on 32-bit boundaries, just like the larger types, with
the
unused 24 or 16 bits simply wasted.

Please ignore the foregoing; I hadn't realized I was in a C# group
rather than a Java group.

Besides the byte type range, then it should be just as valid for C#.
Perhaps. It's possible for the JIT to allocate all of the bytes
together on 8-bit boundaries, all the shorts together on 16-bit
boundaries, etc. I know that Java doesn't do this (at least for
local variables) from the JVM specs; I don't know how one would
determine this in .NET.
Nov 9 '08 #11
Jeroen Mostert wrote:
K Viltersten wrote:
>While i know that the bytes are cheap today,
i still prefer to use a byte (or short) when i know that the entity
counted isn't larger
than 255 (or 65k). However, it's a real pain
to cast every time i perform an operation.

Example:
private byte a, b;
public byte MyProperty
{ get { return (byte)(a + b); } }
It *should* be a pain. You get a good indication of the work the CPU
has to do.
Operating on 32-bit integers is vastly more efficient for a 32-bit
CPU than operating on bytes. The only reason you could have to use
bytes is to save memory, but you have to be pretty tight on that
before you can justify the slowdown in processing. And you have to
show that you're actually *saving* memory: more instructions are
needed to manipulate bytes, and those take up memory too.
All x86 CPUs have 8- and 16-bit versions of most instructions. They may or
may not have longer encodings than the 32-bit ones. Certainly using 8- or
16-bit immediate values is faster than 32-bit immediate values.
>
About the only time you encounter bytes is in byte arrays. Individual
bytes are converted to ints for any nontrivial operation, and they
should stay that way until you store them back.

Public properties should basically never be smaller than an int, it
doesn't pay (unless, again, you can show that you really need the
memory) or when they're part of a predefined interop structure.

Using "short" or "byte" to enforce ranges is a bad idea: it's easy to
throw in a cast to force the value to be in range, but does this actually
produce the correct result? Checking the range explicitly in the
setter is a better idea.
Unless you're on a communication boundary, such as sockets. If the field is
16-bits then better to use a short variable than send something different
from what the user requested.
>
>How can i avoid doing that? I'd like to redeclare/overload the
arithmetical operators so they return a byte (or short)
when computing two bytes (or shorts).
Can't be done in C#. In fact, it can't be done in any .NET language
I'm aware of. Those operations map to CLR primitives and overloading them
from a language would be decidedly strange -- not to mention
inefficient.
While overloading operators on built-in types (all operands are built-in
types) is not possible in any language I know, C++/CLI certainly is a .NET
language that doesn't suffer from the problem of operators performing
unnecessary integral promotion.
Nov 10 '08 #12

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

Similar topics

6
by: woosu | last post by:
Hello ladies and gentlemen. I have a relatively simple problem that I've been unable to solve. In the interest of learning C++, I've decided to write a simple game. The basis for the game is...
2
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
8
by: NilsNilsson | last post by:
I wrote this: short s1 = 0; short s2 = 1; short s3 = s1 + s2; And gor this compile error message: Cannot implicitly convert type 'int' to 'short' What is wrong here?
1
by: Craig Buchanan | last post by:
I am using flag as the identifier for various security settings. For instance: Public Enum SecurityEnum Read=0 Write=1 Delete=2 ... End Enum
7
by: Simon Elliott | last post by:
What operators can be used to extend an enum? Can I extend an enum with an operator= and if so what's the syntax? -- Simon Elliott http://www.ctsn.co.uk
4
by: Ian Richardson | last post by:
Hi, The function I've put together below is a rough idea to extend a SELECT list, starting from: <body> <form name="bambam"> <select id="fred"> <option value="1">1</option> <option...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
6
by: Ashit Vora | last post by:
Hi, I had one small query. I 'm coping data from an unsigned short to a char*. code is #include<stdio.h> int main(void){ unsigned short type=0xFE10;
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.