bitwise-operators

-Wconversion warning while using operator <<= on unsigned char

…衆ロ難τιáo~ 提交于 2020-01-02 01:20:21
问题 When I compile the following code with gcc : int main() { unsigned char c = 1; c <<= 1; // WARNING ON THIS LINE return 0; } I get this warning : conversion to ‘unsigned char’ from ‘int’ may alter its value [-Wconversion] Why ? What is wrong with this code ? Actually, can I really use the oprator <<= on a unsigned char variable ? Compilation command : g++ test.cpp -Wconversion -o test.exe 回答1: This is a valid warning: c <<= 1; is equivalent to: c = c << 1 and the rules for << say that the

Why does bit-wise shift left return different results in Python and Java?

随声附和 提交于 2020-01-01 08:24:15
问题 I'm trying to port some functionality from a Java app to Python. In Java, System.out.println(155 << 24); Returns: -1694498816 In Python: print(155 << 24) Returns 2600468480 Many other bitwise operations have worked in the same way in both languages. Why is there a different result in these two operations? EDIT: I'm trying to create a function in python to replicate how the left shift operator works in Java. Something along the lines of: def lshift(val, n): return (int(val) << n) - 0x100000000

Conditional Statement using Bitwise operators

六月ゝ 毕业季﹏ 提交于 2019-12-31 13:29:09
问题 So I see that this question has already been asked, however the answers were a little vague and unhelpful. Okay, I need to implement a c expression using only "& ^ ~ ! + | >> <<" The expression needs to resemble: a ? b : c So, from what I've been able to tell, the expression needs to look something like: return (a & b) | (~a & c) This works when a = 0, because anding it with b will give zero, and then the or expression will return the right side, (~a & c) which works because ~0 gives all ones

Conditional Statement using Bitwise operators

白昼怎懂夜的黑 提交于 2019-12-31 13:28:15
问题 So I see that this question has already been asked, however the answers were a little vague and unhelpful. Okay, I need to implement a c expression using only "& ^ ~ ! + | >> <<" The expression needs to resemble: a ? b : c So, from what I've been able to tell, the expression needs to look something like: return (a & b) | (~a & c) This works when a = 0, because anding it with b will give zero, and then the or expression will return the right side, (~a & c) which works because ~0 gives all ones

Reading and interpreting data from a binary file in Python

别说谁变了你拦得住时间么 提交于 2019-12-31 10:33:14
问题 I want to read a file byte by byte and check if the last bit of each byte is set: #!/usr/bin/python def main(): fh = open('/tmp/test.txt', 'rb') try: byte = fh.read(1) while byte != "": if (int(byte,16) & 0x01) is 0x01: print 1 else: print 0 byte = fh.read(1) finally: fh.close fh.close() if __name__ == "__main__": main() The error I get is: Traceback (most recent call last): File "./mini_01.py", line 21, in <module> main() File "./mini_01.py", line 10, in main if (int(byte,16) & 0x01) is 0x01

Reading and interpreting data from a binary file in Python

∥☆過路亽.° 提交于 2019-12-31 10:32:22
问题 I want to read a file byte by byte and check if the last bit of each byte is set: #!/usr/bin/python def main(): fh = open('/tmp/test.txt', 'rb') try: byte = fh.read(1) while byte != "": if (int(byte,16) & 0x01) is 0x01: print 1 else: print 0 byte = fh.read(1) finally: fh.close fh.close() if __name__ == "__main__": main() The error I get is: Traceback (most recent call last): File "./mini_01.py", line 21, in <module> main() File "./mini_01.py", line 10, in main if (int(byte,16) & 0x01) is 0x01

Bitwise operators, not vs xor use in branching

梦想与她 提交于 2019-12-31 07:23:12
问题 After asking this SO question, I received a very interesting comment from @AndonM.Coleman that I had to verify. Since your disassembled code is written for x86, it is worth pointing out that XOR will set/clear the Zero Flag whereas NOT will not (sometimes useful if you want to perform a bitwise operation without affecting jump conditions that rely on flags from previous operations). Now, considering you're not writing assembly directly, you really have no access to this flag in a meaningful

int max = ~0; What does it mean?

本秂侑毒 提交于 2019-12-30 23:34:25
问题 int max = ~0; What does it mean? 回答1: The ~ operator is the unary bitwise complement operator which computes the bitwise complement. This means that it reverses all the bits in its argument (0s become 1s and 1s become 0s). Thus, int max = ~0; which is setting max to the negation of the 32-bit value 0000 0000 0000 0000 0000 0000 0000 0000 resulting in 1111 1111 1111 1111 1111 1111 1111 1111 . As we are storing this result in an Int32 , this is the same as -1 . Whether or not it is better to

How many times will the while loop be executed?

半世苍凉 提交于 2019-12-30 09:40:13
问题 I am wondering about how many times this while loop would execute. This is a function that adds two numbers using XOR and AND. def Add(x, y): # Iterate till there is no carry while (y != 0): # carry now contains common # set bits of x and y carry = x & y # Sum of bits of x and y where at # least one of the bits is not set x = x ^ y # Carry is shifted by one so that # adding it to x gives the required sum y = carry << 1 return x `` 回答1: Algorithm for No Carry Adder : function no_carry_adder(A

I want to pack the bits based on arbitrary mask

二次信任 提交于 2019-12-29 07:53:51
问题 Let's say that data is 1011 1001 and the mask is 0111 0110 , then you have: input data: 1011 1001 input mask: 0111 0110 apply mask: 0011 0000 (based on `input mask`) bits selected: -011 -00- (based on `input mask`) right packed: ---0 1100 expected result: 0000 1100 (set left `8 - popcount('input mask')` bits to zero) So the final output is 0000 1100 (note that the 3 positions on the left which are unspecified are zero-filled). You can see that wherever the bits in input mask is 1 the