Is it possible to directly access integer bits in computer memory?
1 次查看(过去 30 天)
显示 更早的评论
Is it possible to directly access integer bits, in computer memory, with MATLAB?
I'm working with arrays of integers, such as [3 8 10 7] for example. Each of these integers is stored as a binary number in the computer. If the integers were 16-bit integers, somewhere in the computer's memory, the numbers have the following binary form:
0000000000000011
0000000000001000
0000000000001010
0000000000000111
I would like to access these 16-bit words directly without having to translate the integers, in the array, into the binary form. Is there some way to do this? After all, they already exist in binary form in memory. The goal is to be able to write programs that are much faster, and more efficient, when it comes to working with integers as binary numbers.
I know that MATLAB has the dec2bin function that will convert the values into character strings that look like the binary form. For example, dec2bin(3,16) would produce a character string that looks like this: '0000000000000011'. However this function not only produces an output that is larger than a simple 16-bit number (since each bit is represented as a character), but it also seems that this function would be slower than simply having access to a form that already exists.
I intend to write a program that switches back and forth between the decimal form and binary form often, and I need it to be fast. Is there any way to switch back and forth without having to go through character string representation? Does MATLAB have any sort of function that will allow me to do this?
Thanks for your time.
0 个评论
回答(4 个)
dpb
2015-10-21
You're probably barking up the wrong tree conceptually in thinking you're going to do anything faster than using straightforward implementation techniques to compute whatever it is that you're out to compute unless you were, perhaps, to switch to implementing it with mex. But, if you're going to do that, what's the point in using a RAD tool such as Matlab at all?
As for the integers as given originally, while they are integer-valued, in Matlab they're stored as doubles so they're not stored in memory in that kind of a model at all. You could, of course, cast to integer via int16 and friends and they will then be stored and can be dealt with as integers. However, with madern CPU, it may in fact actually take more machine cycles to fetch and store them than native-length integers despite the memory savings.
You can, with integers, use bitset, bitget and friends to do bit manipulations but again, don't expect such to be faster than operations on native doubles necessarily.
Perhaps an actual illustration of what you're trying to accomplish could lead to some more promising responses...
0 个评论
Walter Roberson
2015-10-21
No. I haven't seen a bit-sliced general purpose computer for decades, probably not since the Connection Machines of the 1980s. There might be some DSPs still around that provide indexing per bit, but the last one I worked on was a tiny bit of fiddling I was allowed to do in the late 1970s. Arbitrary bit indexing was pretty much gone in mainstream architecture by the days of the PDP-8. Oh wait -- I think it just might have been allowed on some of the Honeywell architectures of the late 1970s, but only in alternate modes I was never permitted to access.
There was a resurgence of interest when RISC chips started to become more popular, as Variable Length Instruction Word (VLIW) architectures were explored. It could even be that underneath some of the processors even now the microcode is VLIW, but as memory sizes grew and Virtual Memory became really important for separating processes (security, stability) and allowing each to access memory without having to worry about writing on each other's memory, the advantages of aligned pages became strong. Later on, the need to maintain Cache Line Coherency in multiprocessor architectures pretty much nailed down the coffin for VLIM architectures. Unless perhaps you work with DSPs, all modern multiprocessors you encounter are word oriented and do not allow indexing by bit (requiring instead that a word be fetched and then logic operations work on relative bit numbers.)
This history lesson has been brought to you by the Rock Of Ages Home For Retired Programmers.
0 个评论
Jan
2015-10-21
Internally Matlab accesses the variables in their binary form. For teh calculation of int16(4) + int16(5) the values are not converted to the decimal representation, but the addition is applied in the processor directly using the binary data. Only for the display in the Command Window the output is converted to the decimal form. Any internal processing, even for floating point types double and single is performed in the binary way directly from the CPU. Therefore trying to accelerate this by converting the values manually between the binary and the decimal format cannot be successful.
Matlab, and other software for numerical computations, handles the data efficient already. It is impossible to find a magic trick to increase the speed of basic arithmetics.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!