bitwise-operators

Get raw bytes of a float in Swift

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 06:01:04
How can I read the raw bytes of a Float or Double in Swift? Example: let x = Float(1.5) let bytes1: UInt32 = getRawBytes(x) let bytes2: UInt32 = 0b00111111110000000000000000000000 I want bytes1 and bytes2 to contain the same value, since this binary number is the Float representation of 1.5 . I need it to do bit-wise operations like & and >> (these are not defined on a float). Update for Swift 3: As of Swift 3, all floating point types have bitPattern property which returns an unsigned integer with the same memory representation, and a corresponding init(bitPattern:) constructor for the

Bitwise operations in class inheriting int

旧巷老猫 提交于 2019-12-04 04:40:52
问题 I've inherited from int, because I wanted to implement a simple interface for bitwise operations. Due to the non mutability of int, I have to use the integer member functions like int.__and__ , ... . class Bitset(int) ... def __setitem__(self, index, value): if value: self.__ior__(1 << int(index)) else: self.__iand__(~(1 << int(index))) In one of my memberfunctions I want to use the |= and &= functions, but the integer has no __ior__ and __iand__ member functions. So my question is how can I

How to use the & operator in C#? Is the the translation of the code correct?

主宰稳场 提交于 2019-12-04 04:20:33
问题 the line "if(arg2 & 1)" in C++(arg2 is DWORD) is equal to "if(arg2 & 1==0)" in C#(arg2 is Uint32),right? I am trying to translate a function from C++ to C#,but I get an error: Operator '&' cannot be applied to operands of type 'uint' and 'bool' I'd be also thankful if you could see further in the whole function for any other mistakes. C++ DWORD Func_X_4(DWORD arg1, DWORD arg2, DWORD arg3) { LARGE_INTEGER result = {1, 0}; LARGE_INTEGER temp1 = {0}; LARGE_INTEGER temp2 = {0}; LARGE_INTEGER

What does the “&=” in this C# code do?

放肆的年华 提交于 2019-12-04 02:58:14
I came across some code that looks like this: string someString; ... bool someBoolean = true; someBoolean &= someString.ToUpperInvariant().Equals("blah"); Why would I use the bitwise operator instead of "="? It's not a bitwise operator when it's applied to boolean operators. It's the same as: someBoolean = someBoolean & someString.ToUpperInvariant().Equals("blah"); You usually see the short-cut and operator && , but the operator & is also an and operator when applied to booleans, only it doesn't do the short-cut bit. You can use the && operator instead (but there is no &&= operator) to

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

荒凉一梦 提交于 2019-12-04 02:13:57
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 However this doesn't seem right as (I think) it turns all numbers negatives? EDIT2: Several hours

To divide by 2 which one is better Right Shift Operator or traditional division operator? [duplicate]

我只是一个虾纸丫 提交于 2019-12-03 21:59:08
This question already has an answer here: Right Shift to Perform Divide by 2 On -1 6 answers While reading Java Source code for Collections.reverse method, Right Shift operator is used for finding middle. ...... for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--) // Right Shift swap(list, i, j); ..... Same can be done by using traditional divide by 2 approach. I explored on stack Right Shift to perform division and find that its better to use division operator and not Right Shift. UPDATE : But then why java guys used Right Shift and not division ? So which approach is better to use and Why ?

Why does XOR swap with integers trigger a warning?

若如初见. 提交于 2019-12-03 18:10:26
问题 I typed the following program: #include <stdio.h> int main(void) { int a = 3; int b = 42; printf("a = %d\nb = %d\n", a, b); printf("Exchanging values.\n"); a ^= b ^= a ^= b; printf("a = %d\nb = %d\n", a, b); return 0; } and it's ok. When I try to compile it, I get this: $ gcc test.c -o test -Wall -Wextra -ansi -pedantic-errors test.c: In function ‘main’: test.c:11: warning: operation on ‘a’ may be undefined That's pretty much standard code, isn't it? Why does it trigger a warning? As far as I

How to combine Intent flags in Kotlin

雨燕双飞 提交于 2019-12-03 14:21:12
问题 I want to combine two intent flags as we do bellow in android Intent intent = new Intent(this, MapsActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); I tried doing something like this but it didn't work for me val intent = Intent(context, MapActivity::class.java) intent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK) 回答1: Explanation: The operation that is applied to the flags is a bitwise or. In Java you have the |

How to overload |= operator on scoped enum?

巧了我就是萌 提交于 2019-12-03 09:27:53
How can I overload the |= operator on a strongly typed (scoped) enum (in C++11, GCC)? I want to test, set and clear bits on strongly typed enums. Why strongly typed? Because my books say it is good practice. But this means I have to static_cast<int> everywhere. To prevent this, I overload the | and & operators, but I can't figure out how to overload the |= operator on an enum . For a class you'd simply put the operator definition in the class , but for enums that doesn't seem to work syntactically. This is what I have so far: enum class NumericType { None = 0, PadWithZero = 0x01, NegativeSign

Check division by 3 with binary operations?

风格不统一 提交于 2019-12-03 08:12:45
I've read this interesting answer about " Checking if a number is divisible by 3 " Although the answer is in Java , it seems to work with other languages also. Obviously we can do : boolean canBeDevidedBy3 = (i % 3) == 0; But the interesting part was this other calculation : boolean canBeDevidedBy3 = ((int) (i * 0x55555556L >> 30) & 3) == 0; For simplicity : 0x55555556L = "1010101010101010101010101010110" Nb There's also another method to check it : One can determine if an integer is divisible by 3 by counting the 1 bits at odd bit positions, multiply this number by 2, add the number of 1-bits