bitwise-operators

Bitwise Shift - Getting different results in C#.net vs PHP

北城以北 提交于 2019-12-13 01:44:55
问题 When I run this command in PHP, I get: Code: 2269495617392648 >> 24 Result: 32 When I run it in C#.net or vb.net, I get: Code: 2269495617392648 >> 24 Result: 135272480 PHP is correct. The interesting thing is, that when I try to shift any number greater than int32 in .net, it yields bad results.. Every number under int32 (2147483647) yields the same results from php and c#.net or vb.net Is there a workaround for this in .net? 回答1: Strictly speaking, PHP is wrong. The entire bit pattern of the

What is the rationale behind (x % 64) == (x & 63)? [duplicate]

我是研究僧i 提交于 2019-12-12 21:01:33
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Bitwise and in place of modulus operator Can someone explain the rationale that makes both expressions equivalents? I know it only works because 64 is a power of two, but how can I logically or mathematically go from division to bitwise and? 回答1: The operation x % 64 returns the remainder when x is divided by 64, which (assuming x>0) must be a number between 0 and 63. Let's look at this in binary: 63 dec = 0011

Removing parts of the image with OpenCV

柔情痞子 提交于 2019-12-12 18:08:36
问题 I have an image and I want to simply remove(or mask) parts of it with OpenCV. this is my original image : And I want to remove a circle on its center via this image mask : I use this command in my code which from the tutorials I read should work and black out a circle in the center of my original image : img = cv2.bitwise_not(imgOriginal,imgOriginal,mask=imgMask) but the result I get is the image below, in fact instead of removing the masked parts, it just inverts blacks and whites: I'll

When should I not use the Java Shortcut Operator &

女生的网名这么多〃 提交于 2019-12-12 12:14:45
问题 My understanding is that when using && as an mathematical operation in Java, if the LHS (Left Hand Side) of an evaluation fails, the right hand side will not be checked, so. false && getName(); getName() would never be called as the LHS has already failed. false & getName(); When would I ever want to check the RHS if I know the LHS has failed? Surely any dependency on the RHS evaluation being ran would be bad coding practice? Thank you 回答1: Well the reason for & and && is not, that one

Is it possible to convert from 4x Uint8 into Uint32 using typed arrays in JavaScript?

假如想象 提交于 2019-12-12 10:44:34
问题 I'm doing some bitwise manipulation in a project and I wonder if the built-in typed arrays might save me some headache and maybe even give me a bit of a performance gain. let bytes = [128, 129, 130, 131] let uint32 = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3] //=> -2138996093 Can I use typed arrays to get the same answer ? // not actually working ! let uint8bytes = Uint8Array.from(bytes) let uint32 = Uint32Array.from(uint8bytes)[0] //=> ideally i'd get the same value as

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

Javascript & PHP Xor equivalent

别等时光非礼了梦想. 提交于 2019-12-11 17:22:07
问题 I have a javascript code: var c = 267414715; var d = c ^ ("0x81BE16CD"); Result is -1907459466 http://jsfiddle.net/4N3JY/1/ I can't seem to get a PHP equivalent. Have tried the following: <?php $c=267414715; $d=$c ^ hexdec("0x81BE16CD"); echo "With hexdec: $d\n"; $d=$c ^ base_convert("0x81BE16CD", 16, 2); echo "With base_convert(2): $d\n"; $d=$c ^ base_convert("0x81BE16CD", 16, 10); echo "With base_convert(10): $d\n"; ?> Output: With hexdec: 2387507830 With base_convert(2):

Bitwise xor python

断了今生、忘了曾经 提交于 2019-12-11 16:03:05
问题 I am trying to solve a problem where I have to decrypt a file. But I found an obstacle. As you can see in the below code, I need to do a bitwise xor between key and number 47. from Crypto.Cipher import AES import base64 l1 = open("./2015_03_13_mohamed.said.benmousa.puerta_trasera.enc", "rb"); iv = l1.read(16) enc = l1.read() l1.close() #key = xor beetwen kiv(47) and IV key = iv for i in range(len(key)): key[i] = key[i] ^ 47 obj = AES.new(key,AES.MODE_CBC, iv) obj1 = obj.decrypt(enc) l = open(

Changing specific set of bits in a byte

自古美人都是妖i 提交于 2019-12-11 15:03:21
问题 I am working on a function that receives a byte and needs to change some of the bits in that byte. For example, the function receives: 11001011 Then I need to set the MSB to 0, its easy enough: buffer[0] &= ~(1 << 7); But then I need to set bits 6 through 3 (I refer LSB as bit 0 here) to an argument that gets supplied to the function. This argument can be an integer from 0 to 6 . The important thing is I should not change any other bits. I tried with masking and stuff but I failed miserably.