- Bitwise chunks of memory together.
- Posted by brigham white on December 30th, 2003
Few questions here.
Is there a way to bitwise two memory chunks together that are larger than
the largest primitive type built into c++? Or in other words, if I have two
pointers to arbitrarily sized memory chunks that are equal to eachother and
exceed say 32 bits.. can I bitwise those two memory chunks together? I
assume that the memory chunks would have to be in multiples of the size of a
byte. In my app, the chunk of memory would require me to use 176 bitwise
operations at 32 bits at a time to obtain the results I want. Are bitwise
operations costly in cpu time? Thanks..
- Posted by Jeff Schwab on December 31st, 2003
brigham white wrote:
Loop.
Perhaps some languages have built-in operators or standard library
functions for this, but I don't know of any processor that has a
built-in "xor these two arbitrarily sized chunks of memory" instruction.
A chip meant specifically to perform simple encryption might. At any
rate, if your code is running on a typical processor, just write the
loop and stick it in a function. If the performance is poor, come back
and ask for advice.
Sorry I couldn't give you better news. Perhaps someone will enlighten
me about a hitherto unnoticed instruction common to the popular
architectures.
-Jeff
- Posted by Josh Sebastian on December 31st, 2003
On Tue, 30 Dec 2003 18:15:35 -0600, brigham white wrote:
std::bitset
Josh
- Posted by Gerry Quinn on December 31st, 2003
In article <1uqdnT31Uqf5uW-iRVn-iQ@comcast.com>, Jeff Schwab <jeffplus@comcast.net> wrote:
I would guess that all standard processors are very fast with bitwise
operators, i.e. the same speed as integer addition and subtraction, and
probably among the fastest instructions on the chip (depending on how
the bits are accessed).
Gerry Quinn
--
http://bindweed.com
Screensavers, Games, Kaleidoscopes
Download free trial versions
- Posted by Jeff Schwab on December 31st, 2003
Gerry Quinn wrote:
Agreed. However, I don't believe those operators can work on
arbitrarily sized chunks of memory. Typically, they work on only a
small number of bytes (perhaps eight) at a time. To xor large chunks of
memory requires many executions of the local "xor" instruction. Xor
actually may be faster than addition or substraction for large blocks of
memory, since the carry bit is not needed.
-Jeff
- Posted by Martijn Lievaart on January 1st, 2004
On Tue, 30 Dec 2003 18:15:35 -0600, brigham white wrote:
Very easy, basic C. Just write a loop that iterates through the blocks of
memory and applies the operation.
Why 32 bits at a time? Binary operations tend to give the same result
however you partition it.
Bitwise operations are what computers are very good at, so I don't expect
any problems there. You may run into problems if you are doing, say
runtime video operations or heavy crypto operations.
If performance really is a problem, some chips have specialised
instructions, like Intel-MMX. However that would be very OT for this group
and you actually only need it if you're doing very specialised work that
needs stellar real-time performance, iow, probably not you. Besides, your
question is so basic I suggest you stay away from this low-level
programming for at least another year. With modern CPUs and C/C++
compilers you are very, very hardpressed to come up with a better
hand-crafted solution than your compiler can create from your source.
[ Way back I had written this program where I had to wring out the very
last drop of performance. Even without profiling I knew where the
bottleneck was, a function with nested loops that was called a trillion
times. So I try to rewrite it in assembly, starting with what the compiler
made of it. I was /not/ able to improve over the compiler. Modern
compilers are even better and modern CPUs even more difficultl to hand
optimise for. ]
HTH,
M4