473,564 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10500
da*********@gma il.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.d kwrote:
darthgha...@gma il.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...@gma il.com wrote:
On Feb 10, 12:05 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
darthgha...@gma il.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...@cana da.comwrote:
On Feb 12, 2:54 pm, darthgha...@gma il.com wrote:
On Feb 10, 12:05 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
darthgha...@gma il.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*******@cana da.comwrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
On Feb 12, 2:54 pm, darthgha...@gma il.com wrote:
On Feb 10, 12:05 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
darthgha...@gma il.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.d kwrote in message
news:45******** *************** @news.sunsite.d k...
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.no spamwrote:
"Arne Vajhøj" <a...@vajhoej.d kwrote in message

news:45******** *************** @news.sunsite.d k...
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...@gma il.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

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

Similar topics

15
2315
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
3708
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 other storage otherwise. Rather than lose space to padding by just using a sentinal bool to indicate which member of the union has been written...
7
2109
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 complement of '2'? or is '-c' (two's complement of c) computed on the fly and the resulting value is added to b ( b + (-c))?
3
1881
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 { byte pB = new byte;
6
1593
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, what is the most efficient method? Sincerely, José
3
13136
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 work but doesn't: byteArray &= ~0xe0; // set the first three bits to 0 (11100000 == 0xe0) The error is "Constant value '-225' cannot be converted...
14
1836
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 ((byte<<24)|(byte<<16)|(byte<<8)|(byte)); } /* part of main funciton */ ifstream fp("in.bin",ios::binary); char buff;
2
7895
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
5645
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) On each FileSystemItem Element, make two child nodes, one with the file name, one with the file size. ie. <filesystemitems> <filesystemitem>
0
7665
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...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
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...
0
6255
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...
1
5484
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...
1
2082
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 we have to send another system

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.