bit-shift

Finding an efficient shift/add/LEA instruction sequence to multiply by a given constant (avoiding MUL/IMUL)

假装没事ソ 提交于 2020-02-24 09:59:45
问题 I'm trying to write a C program mult.c that has a main function that receives 1 int argument (parsed with atoi(argv[1]) ), that is some constant k we want to multiply by. This program will generate an assembly file mult.s that implements int mult(int x) { return x * k; } for that constant k . (This is a followup to Efficient Assembly multiplication) For example: if main() in mult.c gets 14 as argument it may generate (though it is not minimal as later emphasized): .section .text .globl mult

using bit shifting variables inside if statement - error or not

前提是你 提交于 2020-01-26 03:59:23
问题 Suppose we have some variables x and y, and the following if statement which involves bit shifting: if (x<<y) I've read some posts which also deal with the issue of using bit shifting with variables (of some type) and inside if statement, but unfortunately I haven't been able to reach a unequivocal conclusion whether it is an error or not. I assume that if it is an error, then it's a semantic error or a run-time error . But is it necessarily en error ? 回答1: If x is of an unsigned integer type

Finding the absolute value of a number in 8085 microprocessor assembly language

霸气de小男生 提交于 2020-01-24 12:11:25
问题 I have a task of finding the absolute value of any given number in 8085 assembly language. The algorithm is the following (found on the internet): mask = n >> 7 (number itself is 8 bits) (mask + n) XOR mask My question is that how would I implement this in assembly language. It seems that I should be using the "RRC" command but that performs circular shift on the number and the algorithm doesnt seem to work. Any ideas would be appreciated. Cheers. 回答1: The n>>7 in that abs algorithm is an

Converting 24 bit integer (2s complement) to 32 bit integer in C++

孤者浪人 提交于 2020-01-23 17:08:06
问题 The dataFile.bin is a binary file with 6-byte records. The first 3 bytes of each record contain the latitude and the last 3 bytes contain the longitude. Each 24 bit value represents radians multiplied by 0X1FFFFF This is a task I've been working on. I havent done C++ in years so its taking me way longer than I thought it would -_-. After googling around I saw this algorthim which made sense to me. int interpret24bitAsInt32(byte[] byteArray) { int newInt = ( ((0xFF & byteArray[0]) << 16) | (

Shifting left in MIPS

北慕城南 提交于 2020-01-21 10:20:27
问题 In my method one, I have the number that I want to shift by stored inside $a0 (e.g. 5 bits), and I want to shift $t9 by 5 bits, but I'm running into a bit of trouble. Does anyone know why? MethodOne: sw $a0, ($t8) sll $t9, $t9, $t8 回答1: To shift left by a variable amount, use sllv : sllv $t9, $t9, $a0 sll only takes an immediate shift amount. There is no need for sw or $t8 . 来源: https://stackoverflow.com/questions/26051933/shifting-left-in-mips

Unsigned left shift in VB.NET?

断了今生、忘了曾经 提交于 2020-01-21 09:12:27
问题 This should be an easy one for folks, but how do I pull off an unsigned left shift in VB.NET while using Option Strict ? Maybe I am doing it wrong, but while trying to implement my own IP2Long function (I have my reasons), I'm testing things to make sure I have my head wrapped around the conversion process properly. I tried a few tests, and all seem to cause errors. Dim a As Int32 a = CUint(172 << 24) 'Constant expression not representable in type 'UInteger' a = DirectCast((172 << 24), UInt32

Why is BitConverter slower than doing the bitwise operations directly?

冷暖自知 提交于 2020-01-16 19:18:12
问题 I recently did some profiling on some code and found that the largest CPU usage was being consumed by calls to BitConverter such as: return BitConverter.ToInt16(new byte[] { byte1, byte2 }); when switching to something like: return (short)(byte1 << 8 | byte2); I noticed a huge improvement in performance. My question is why is using BitConverter so much slower? I would have assumed that BitConverter was essentially doing the same kind of bit shifting internally. 回答1: The call to BitConverter

Mysteries of C++ optimization

倖福魔咒の 提交于 2020-01-14 10:13:49
问题 Take the two following snippets: int main() { unsigned long int start = utime(); __int128_t n = 128; for(__int128_t i=1; i<1000000000; i++) n = (n * i); unsigned long int end = utime(); cout<<(unsigned long int) n<<endl; cout<<end - start<<endl; } and int main() { unsigned long int start = utime(); __int128_t n = 128; for(__int128_t i=1; i<1000000000; i++) n = (n * i) >> 2; unsigned long int end = utime(); cout<<(unsigned long int) n<<endl; cout<<end - start<<endl; } I am benchmarking 128 bit

PHP Unsigned Right Shift - Malfunctioning

限于喜欢 提交于 2020-01-13 10:07:29
问题 So, when using my method to preform a ( >>> ) unsigned right shift in PHP, the result is incorrect when the numbers involve negatives. PHP Application Results: INPUT: 10 >>> 3 INPUT: -10 >>> 3 OUTPUT: 1 OUTPUT: 2684354558 JAVA APPLICATION RESULTS: INPUT: 10 >>> 3 INPUT: -10 >>> 3 OUTPUT: 1 OUTPUT: 536870910 (The top results are correct and generated by Java and then the bottom results are incorrect and generated by PHP) It's only when the number is negative in PHP that it fails. The shifts

SHR and SAR Commands

喜欢而已 提交于 2020-01-11 14:25:10
问题 I would like to make sure I am understanding this concept 100% and if not get some clarification. In an asm program, if you perform SHR 00110000b you would end up with 00011000b . However, if you were to perform SHR on 11111111b you would end up with an incorrect answer and should use SAR instead? This is because the number is signed? 回答1: if you perform SHR 00110000b you would end up with 00011000b If you shifted one bit to the right, yes. You can specify the shift amount, so it's not fixed