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

efficient two's complement of a byte array

What would be the most efficient way to calculate the two's complement
of a variable length byte array?
Thanks for your time.

Feb 10 '07 #1
14 10459
da*********@gmail.com wrote:
What would be the most efficient way to calculate the two's complement
of a variable length byte array?
I would assume a for loop negating would be good enough
for most purposes.

You could try negating int or long using unsafe code and
a pointer.

Arne
Feb 10 '07 #2
On Feb 10, 12:05 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
darthgha...@gmail.com wrote:
What would be the most efficient way to calculate the two's complement
of a variable length byte array?

I would assume a for loop negating would be good enough
for most purposes.

You could try negating int or long using unsafe code and
a pointer.

Arne
I can't negate int or long because the number could be over 4 billion
digits (in hex). A for loop would do the trick, but I was hoping
someone might know of a different way. Thanks for the thoughts
though.
Chris

Feb 12 '07 #3
On Feb 12, 2:54 pm, darthgha...@gmail.com wrote:
On Feb 10, 12:05 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
darthgha...@gmail.com wrote:
What would be the most efficient way to calculate the two's complement
of a variable length byte array?
I would assume a for loop negating would be good enough
for most purposes.
You could try negating int or long using unsafe code and
a pointer.
Arne

I can't negate int or long because the number could be over 4 billion
digits (in hex). A for loop would do the trick, but I was hoping
someone might know of a different way. Thanks for the thoughts
though.
What Arne was suggesting was an old C trick: looping over the array
one long (64 bits) at a time, negating each long, rather than doing it
a byte at a time. However, as he pointed out, you would have to use
unsafe code to do this.

Feb 13 '07 #4
On Feb 12, 4:56 pm, "Bruce Wood" <brucew...@canada.comwrote:
On Feb 12, 2:54 pm, darthgha...@gmail.com wrote:
On Feb 10, 12:05 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
darthgha...@gmail.com wrote:
What would be the most efficient way to calculate the two's complement
of a variable length byte array?
I would assume a for loop negating would be good enough
for most purposes.
You could try negating int or long using unsafe code and
a pointer.
Arne
I can't negate int or long because the number could be over 4 billion
digits (in hex). A for loop would do the trick, but I was hoping
someone might know of a different way. Thanks for the thoughts
though.

What Arne was suggesting was an old C trick: looping over the array
one long (64 bits) at a time, negating each long, rather than doing it
a byte at a time. However, as he pointed out, you would have to use
unsafe code to do this.
Thanks for clarifying that. I could do that, but I am trying to avoid
unsafe code. Also, this is a school project and I would get lots of
objections to that.
Thanks for the time guys,
Chris

Feb 13 '07 #5

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
On Feb 12, 2:54 pm, darthgha...@gmail.com wrote:
On Feb 10, 12:05 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
darthgha...@gmail.com wrote:
What would be the most efficient way to calculate the two's complement
of a variable length byte array?
I would assume a for loop negating would be good enough
for most purposes.
You could try negating int or long using unsafe code and
a pointer.
Arne
While working on the native word size of the processor should be faster, it
won't work the way you suggest, because negating (two's complement) is one's
complement which is independent of word size, followed by adding one. That
adding one step won't be done right for simple negation of a wide int, and
adding the right bit pattern (0x01010101) is going to have an unfortunate
tendency to carry from one byte into the next. SIMD (i.e. MMX, SSE or
3DNow!) instructions are your friend.
Feb 13 '07 #6
Ben Voigt wrote:
While working on the native word size of the processor should be faster, it
won't work the way you suggest, because negating (two's complement) is one's
complement which is independent of word size, followed by adding one. That
adding one step won't be done right for simple negation of a wide int, and
adding the right bit pattern (0x01010101) is going to have an unfortunate
tendency to carry from one byte into the next.
You are right. For two's complement we can not change size of items
manipulated.

Can we please bring back ones complement computers.

:-)
SIMD (i.e. MMX, SSE or
3DNow!) instructions are your friend.
Then we just need the asm keyword.

:-)

Arne
Feb 13 '07 #7

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:45***********************@news.sunsite.dk...
Ben Voigt wrote:
>While working on the native word size of the processor should be faster,
it won't work the way you suggest, because negating (two's complement) is
one's complement which is independent of word size, followed by adding
one. That adding one step won't be done right for simple negation of a
wide int, and adding the right bit pattern (0x01010101) is going to have
an unfortunate tendency to carry from one byte into the next.

You are right. For two's complement we can not change size of items
manipulated.

Can we please bring back ones complement computers.

:-)
SIMD (i.e. MMX, SSE or
3DNow!) instructions are your friend.

Then we just need the asm keyword.
In .NET, it would be the jitter's responsibility to apply SIMD instructions
where possible. This might reasonable require some preparation, such as
having the loop unrolled to the right number of simultaneous steps, but I
think the jitter is supposed to do loop unrolling as well.

C++ typically has intrinsic functions for access to advanced instructions,
and some people built template libraries to expose vector classes which use
SIMD for operations, decoding to the native instruction set so the client
code is portable... if C++ can do without inline assembler, then surely C#
can as well since it's Microsoft's gift to the world....
>
:-)

Arne

Feb 13 '07 #8
On Feb 13, 12:34 pm, "Ben Voigt" <r...@nospam.nospamwrote:
"Arne Vajhøj" <a...@vajhoej.dkwrote in message

news:45***********************@news.sunsite.dk...
Ben Voigt wrote:
While working on the native word size of the processor should be faster,
it won't work the way you suggest, because negating (two's complement)is
one's complement which is independent of word size, followed by adding
one. That adding one step won't be done right for simple negation of a
wide int, and adding the right bit pattern (0x01010101) is going to have
an unfortunate tendency to carry from one byte into the next.
You are right. For two's complement we can not change size of items
manipulated.
Can we please bring back ones complement computers.
:-)
SIMD (i.e. MMX, SSE or
3DNow!) instructions are your friend.
Then we just need the asm keyword.

In .NET, it would be the jitter's responsibility to apply SIMD instructions
where possible. This might reasonable require some preparation, such as
having the loop unrolled to the right number of simultaneous steps, but I
think the jitter is supposed to do loop unrolling as well.

C++ typically has intrinsic functions for access to advanced instructions,
and some people built template libraries to expose vector classes which use
SIMD for operations, decoding to the native instruction set so the client
code is portable... if C++ can do without inline assembler, then surely C#
can as well since it's Microsoft's gift to the world....
:-)
Arne
So what you are all saying is that there is no easy way to get a two's
complement of a byte array? That sucks. I guess that's Microsoft's
gift to me?
Thanks again for your time.
Chris

Feb 17 '07 #9
On Feb 16, 8:54 pm, darthgha...@gmail.com wrote:
So what you are all saying is that there is no easy way to get a two's
complement of a byte array? That sucks. I guess that's Microsoft's
gift to me?
I don't know if it's particularly _Microsoft's_ gift to you. More like
the modern Intel processor's gift to you. In fact, I don't think I've
ever worked on a language or a processor in which something as obscure
as taking the two's-complement of a byte array was optimized into some
special instruction or operation.

There's certainly an _easy_ way to do it: loop through the bytes and
take the two's complement of each one. That's pretty darned easy.

However, what you asked for was _faster_. As I said, I've never seen a
_fast_, short-cut way to do that, on any platform, on any chip.
Perhaps others with wider experience have, but not me.

Feb 17 '07 #10
On Feb 16, 11:44 pm, "Bruce Wood" <brucew...@canada.comwrote:
On Feb 16, 8:54 pm, darthgha...@gmail.com wrote:
So what you are all saying is that there is no easy way to get a two's
complement of a byte array? That sucks. I guess that's Microsoft's
gift to me?

I don't know if it's particularly _Microsoft's_ gift to you. More like
the modern Intel processor's gift to you. In fact, I don't think I've
ever worked on a language or a processor in which something as obscure
as taking the two's-complement of a byte array was optimized into some
special instruction or operation.

There's certainly an _easy_ way to do it: loop through the bytes and
take the two's complement of each one. That's pretty darned easy.

However, what you asked for was _faster_. As I said, I've never seen a
_fast_, short-cut way to do that, on any platform, on any chip.
Perhaps others with wider experience have, but not me.
Thanks for the response. That was the way I was hoping to avoid.
Looks like it's the best bet.
Thanks again for the time.
Chris

Feb 17 '07 #11
On Feb 17, 12:16 am, darthgha...@gmail.com wrote:
On Feb 16, 11:44 pm, "Bruce Wood" <brucew...@canada.comwrote:
On Feb 16, 8:54 pm, darthgha...@gmail.com wrote:
So what you are all saying is that there is no easy way to get a two's
complement of a byte array? That sucks. I guess that's Microsoft's
gift to me?
I don't know if it's particularly _Microsoft's_ gift to you. More like
the modern Intel processor's gift to you. In fact, I don't think I've
ever worked on a language or a processor in which something as obscure
as taking the two's-complement of a byte array was optimized into some
special instruction or operation.
There's certainly an _easy_ way to do it: loop through the bytes and
take the two's complement of each one. That's pretty darned easy.
However, what you asked for was _faster_. As I said, I've never seen a
_fast_, short-cut way to do that, on any platform, on any chip.
Perhaps others with wider experience have, but not me.

Thanks for the response. That was the way I was hoping to avoid.
Looks like it's the best bet.
Thanks again for the time.
Chris
Looks like the newest version of Visual Studio will come with a
BigInteger class that I could use. It doesn't support bitwise right
now, but may be they will add it later.
Here's where I saw that:
http://blogs.msdn.com/bclteam/archiv...bar-gazit.aspx

Feb 21 '07 #12
On Feb 20, 8:25 pm, darthgha...@gmail.com wrote:
On Feb 17, 12:16 am, darthgha...@gmail.com wrote:


On Feb 16, 11:44 pm, "Bruce Wood" <brucew...@canada.comwrote:
On Feb 16, 8:54 pm, darthgha...@gmail.com wrote:
So what you are all saying is that there is no easy way to get a two's
complement of a byte array? That sucks. I guess that's Microsoft's
gift to me?
I don't know if it's particularly _Microsoft's_ gift to you. More like
the modern Intel processor's gift to you. In fact, I don't think I've
ever worked on a language or a processor in which something as obscure
as taking the two's-complement of a byte array was optimized into some
special instruction or operation.
There's certainly an _easy_ way to do it: loop through the bytes and
take the two's complement of each one. That's pretty darned easy.
However, what you asked for was _faster_. As I said, I've never seen a
_fast_, short-cut way to do that, on any platform, on any chip.
Perhaps others with wider experience have, but not me.
Thanks for the response. That was the way I was hoping to avoid.
Looks like it's the best bet.
Thanks again for the time.
Chris

Looks like the newest version of Visual Studio will come with a
BigInteger class that I could use. It doesn't support bitwise right
now, but may be they will add it later.
Here's where I saw that:http://blogs.msdn.com/bclteam/archiv...ucing-system-n...
Sorry... still won't work. Again, if you want ones-complement then
this is fine. If you want twos-complement then it won't fly: you have
to take twos-complement byte by byte.

Feb 21 '07 #13

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
On Feb 16, 8:54 pm, darthgha...@gmail.com wrote:
>So what you are all saying is that there is no easy way to get a two's
complement of a byte array? That sucks. I guess that's Microsoft's
gift to me?

I don't know if it's particularly _Microsoft's_ gift to you. More like
the modern Intel processor's gift to you. In fact, I don't think I've
ever worked on a language or a processor in which something as obscure
as taking the two's-complement of a byte array was optimized into some
special instruction or operation.
PSUBSB?

http://asm.inightmare.org/opcodelst/index.php?op=PSUBSB
>
There's certainly an _easy_ way to do it: loop through the bytes and
take the two's complement of each one. That's pretty darned easy.

However, what you asked for was _faster_. As I said, I've never seen a
_fast_, short-cut way to do that, on any platform, on any chip.
Perhaps others with wider experience have, but not me.

Feb 21 '07 #14

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:uA**************@TK2MSFTNGP04.phx.gbl...
>
"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
>On Feb 16, 8:54 pm, darthgha...@gmail.com wrote:
>>So what you are all saying is that there is no easy way to get a two's
complement of a byte array? That sucks. I guess that's Microsoft's
gift to me?

I don't know if it's particularly _Microsoft's_ gift to you. More like
the modern Intel processor's gift to you. In fact, I don't think I've
ever worked on a language or a processor in which something as obscure
as taking the two's-complement of a byte array was optimized into some
special instruction or operation.

PSUBSB?

http://asm.inightmare.org/opcodelst/index.php?op=PSUBSB
Sorry, if you want true two's-complement, don't use non-saturating PSUBB

http://asm.inightmare.org/opcodelst/index.php?op=PSUBB

If you want arithmetic negation, PSUBSB more likely serves your purpose.
Feb 21 '07 #15

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

Similar topics

15
by: Tor Erik Sønvisen | last post by:
Hi I need a time and space efficient way of storing up to 6 million bits. Time efficency is more important then space efficency as I'm going to do searches through the bit-set. regards tores
2
by: Thomas Paul Diffenbach | last post by:
I'm trying to write a space efficient string (nul terminated array of char), storing the characters directly unless the number of characters is too large to be so stored, and storing a pointer to...
7
by: Greenhorn | last post by:
Hi, Is two's complement always used as a storage method or is it computed while computing the expression involved. e.g., int a = -2, b = 3, c = 4, d; d = b - c; Here, is 'a' stored as two's...
3
by: Ted Miller | last post by:
Hi folks, I've got an unmanaged routine I'm pinvoking to. It takes a pointer to an array of 3 pointers to bytes, typed as byte**. public static extern void foo(byte **p3pb); unsafe {...
6
by: José Joye | last post by:
I have to compare 2 byte and I must be sure that they are fully identic. I have to perform this check about 1000 times per minute and on arrays that are between 100-200K in size. In that sense,...
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
14
by: KK | last post by:
/* Target - read an integer from a binary file */ unsigned int Byte2Int(char *buff) { unsigned char* byte = reinterpret_cast<unsigned char*> (buff); return...
2
by: chris | last post by:
I have a few byte arrays that I would like to combine into one array (order needs to be kept). What would be the most efficient way to do this? Thanks for your time, Chris
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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...

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.