443,652 Members | 1,337 Online
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

# efficient two's complement of a byte array

 P: n/a 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 Replies

 P: n/a 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

 P: n/a On Feb 10, 12:05 pm, Arne Vajhøj

 P: n/a On Feb 12, 2:54 pm, darthgha...@gmail.com wrote: On Feb 10, 12:05 pm, Arne Vajhøj

 P: n/a On Feb 12, 4:56 pm, "Bruce Wood"

 P: n/a "Bruce Wood"

 P: n/a 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

 P: n/a "Arne Vajhøj" 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) isone's complement which is independent of word size, followed by addingone. That adding one step won't be done right for simple negation of awide int, and adding the right bit pattern (0x01010101) is going to havean 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 or3DNow!) 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

 P: n/a On Feb 13, 12:34 pm, "Ben Voigt"

 P: n/a 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

 P: n/a On Feb 16, 11:44 pm, "Bruce Wood"

 P: n/a On Feb 17, 12:16 am, darthgha...@gmail.com wrote: On Feb 16, 11:44 pm, "Bruce Wood"

 P: n/a 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"

 P: n/a "Bruce Wood" So what you are all saying is that there is no easy way to get a two'scomplement of a byte array? That sucks. I guess that's Microsoft'sgift 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

 P: n/a "Ben Voigt" "Bruce Wood" 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'scomplement of a byte array? That sucks. I guess that's Microsoft'sgift to me? I don't know if it's particularly _Microsoft's_ gift to you. More likethe modern Intel processor's gift to you. In fact, I don't think I'veever worked on a language or a processor in which something as obscureas taking the two's-complement of a byte array was optimized into somespecial 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 discussion thread is closed

Replies have been disabled for this discussion.