bit-shift

Large binary shifts in 8086 assembly?

六月ゝ 毕业季﹏ 提交于 2019-12-13 16:07:05
问题 I have a binary chunk of data 512 bytes long, I was wondering what the most efficient way would be if I wanted to shift it once to the right. My best guess right now (very new to assembly) would be that I would have to first check a chunk (probably int) to see what it would shift out, shift, then carry in whatever the previous int would have shifted out and proceed carrying this shift down the data. Is there an easier way? If I have to use this carry technique, what's the largest chunk I can

Why doesn't this swap macro using shifts not work for negative numbers?

让人想犯罪 __ 提交于 2019-12-13 15:56:47
问题 I found some code in a built in library that I have and need to extend. But it appears to be broken: #define BSWAP16(__x) ((((__x) >> 8) | ((__x) << 8))) Does not function the same as: __builtin_bswap16() This program proves it. #include <stdio.h> #define BSWAP16(__x) ((((__x) >> 8) | ((__x) << 8))) int main(int argc, char* argv[]) { unsigned short a = (unsigned short)BSWAP16(0xff00); unsigned short b = __builtin_bswap16(0xff00); short c = (short)BSWAP16(-8); short d = __builtin_bswap16(-8);

Why does shifting 0xff left by 24 bits result in an incorrect value?

南楼画角 提交于 2019-12-13 09:08:04
问题 I would like to shift 0xff left by 3 bytes and store it in a uint64_t , which should work as such: uint64_t temp = 0xff << 24; This yields a value of 0xffffffffff000000 which is most definitely not the expected 0xff000000 . However, if I shift it by fewer than 3 bytes, it results in the correct answer. Furthermore, trying to shift 0x01 left by 3 bytes does work. Here's my output: 0xff shifted by 0 bytes: 0xff 0x01 shifted by 0 bytes: 0x1 0xff shifted by 1 bytes: 0xff00 0x01 shifted by 1 bytes

How to cast from int to byte, then use a bitshift operator

烈酒焚心 提交于 2019-12-13 06:24:35
问题 Why does the following not work? I cast an int to a byte, then shift the bits over by 7. I don't see any problems there. However, I get the error message "possible loss of precision... required: byte; found: int" pixels is an array of bytes, c is a Color object, iter is an integer. pixels[iter++] = ((byte) c.getRed()) << 7; pixels[iter++] = ((byte) c.getGreen()) << 7; pixels[iter++] = ((byte) c.getBlue()) << 7; 回答1: In Java, the shift operators return an int value, even if the quantity being

How do i do a bitshift right in binary?

天涯浪子 提交于 2019-12-13 05:42:36
问题 Hopefully this is a simple question but I cannot for the life of me figure out how to do a bitshift in binary. This is being done in the LC3 environemnt. I just need to know how to arithmetical divide by two and shift to the right. I know going left is simple by just adding the binary value to itself, but I have tried the opposite for bitshift right(subtracting from itself, NOTing and then subtracting etc etc.) Would be much appreciated. Or if you have a better way to move x00A0 to x000A that

Shifting negative BigInteger value - Java

試著忘記壹切 提交于 2019-12-13 05:13:23
问题 I am trying to shift a 7 byte array to the right by 7 bits. To do this, I am using BigInteger's shiftright method. However, when shifting right for negative BigIntegers, padding with 1s are added or sometimes the leading bit is removed. Here is the following bit of code doing the shifting: byte[] vcwManD = decryptedVCW; BigInteger bigIntD = new BigInteger(vcwManD); // create big int array for shift BigInteger shiftIntD= bigIntD.shiftRight(7); // shift right 7 bits vcwManD = shiftIntD

Get the same shift left in Python as Java

心已入冬 提交于 2019-12-12 18:09:34
问题 Specifically I want to take this number: x = 1452610545672622396 and perform x ^= (x << 21) // In Python I do x ^= (x << 21) & 0xffffffffffffffff I want to get: -6403331237455490756 , which is what I get in Java instead of: 12043412836254060860 , which is what I get in Python ( which is what I don't want ) EDIT: In Java I do: long x = 1452610545672622396; x ^= (x << 21); 回答1: You can use 64bit signed int like java using ctypes.c_longlong, please see example below: from ctypes import c

C/C++: Multiply, or bitshift then divide? [duplicate]

╄→гoц情女王★ 提交于 2019-12-12 10:37:02
问题 This question already has answers here : Is multiplication and division using shift operators in C actually faster? (18 answers) Closed 5 years ago . Where it's possible to do so, I'm wondering if it's faster to replace a single multiplication with a bitshift followed by an integer division. Say I've got an int k and I want to multiply it by 2.25. What's faster? int k = 5; k *= 2.25; std::cout << k << std::endl; or int k = 5; k = (k<<1) + (k/4); std::cout << k << std::endl; Output 11 11 Both

difference between JavaScript bit-wise operator code and Python bit-wise operator code

谁说胖子不能爱 提交于 2019-12-12 09:55:20
问题 I have converted JavaScript code which uses bit-wise operators in that code to Python code, but there is one problem when i do this in JavaScript and Python 412287 << 10 then I get this 422181888 same results in both languages. but when i do this in both 424970184 << 10 then i get different results in both of the languages 1377771520 in JavaScript and 435169468416 in Python can anybody help me with this? any help would be appreciated. 回答1: If you want the java-script equivalent value then

What happens with bitwise shift for all 8 bits

强颜欢笑 提交于 2019-12-12 09:41:17
问题 I have a small query in c, I am using the bitwise left shift on number 69 which is 01000101 in binary 01000101 << 8 and I get answer as 100010100000000 Shouldn't it be all 8 zeros i.e. 00000000 as we shift all the 8 bits to left and then pad with zeros. 回答1: It is because of the literal (default data type) for a number ( int ) is, in most of nowadays CPU, greater than 8-bit (typically 32-bit ) and thus when you apply 69 << 8 //note 69 is int It is actually applied like this 00000000 00000000