bitwise-operators

Split UInt32 into [UInt8] in swift

只谈情不闲聊 提交于 2019-12-19 06:22:15
问题 I want to add UInt32 to byte buffer for which I use [UInt8] . In java, there is convenient ByteBuffer class that has methods like putInt() for cases exactly like this. How could this be done in swift? I guess I could solve this as following: let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15 var byteArray = [UInt8](count: 4, repeatedValue: 0) for i in 0...3 { byteArray[i] = UInt8(0x0000FF & example >> UInt32((3 - i) * 8)) } This is quite verbose though, any simpler way? 回答1: Your loop

Split UInt32 into [UInt8] in swift

给你一囗甜甜゛ 提交于 2019-12-19 06:22:12
问题 I want to add UInt32 to byte buffer for which I use [UInt8] . In java, there is convenient ByteBuffer class that has methods like putInt() for cases exactly like this. How could this be done in swift? I guess I could solve this as following: let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15 var byteArray = [UInt8](count: 4, repeatedValue: 0) for i in 0...3 { byteArray[i] = UInt8(0x0000FF & example >> UInt32((3 - i) * 8)) } This is quite verbose though, any simpler way? 回答1: Your loop

Type safe enum bit flags

↘锁芯ラ 提交于 2019-12-19 05:17:20
问题 I'm looking to use a set of bit flags for my current issue. These flags are (nicely) defined as part of an enum , however I understand that when you OR two values from an enum the return type of the OR operation has type int . What I'm currently looking for is a solution which will allow the users of the bit mask to remain type safe, as such I have created the following overload for operator | enum ENUM { ONE = 0x01, TWO = 0x02, THREE = 0x04, FOUR = 0x08, FIVE = 0x10, SIX = 0x20 }; ENUM

Why doesn't C have rotate left/right operators? [closed]

纵饮孤独 提交于 2019-12-19 05:16:20
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . A bit of a philosophical question, I suppose. The C language has the standard set of bit-wise operations, including OR , AND , XOR ,

Python list to bitwise operations

流过昼夜 提交于 2019-12-18 15:35:19
问题 Is there a way to take a list of django query expresses (e.g. Q(first_name="Jordan") , where Q is django.db.models.Q ) and bitwise OR them together? In other words, I have something like this: search_string = "various search terms" And I want to do this: search_params = [Q(description__icontains=term) for term in re.split(r'\W', search_string)] search_params = something_magical(search_params) results = Record.objects.filter(search_params) where search_params now is equivalent to Q(description

How does the bitwise operator XOR ('^') work?

半城伤御伤魂 提交于 2019-12-18 12:47:34
问题 I'm a little confused when I see the output of following code: $x = "a"; $y = "b"; $x ^= $y; $y ^= $x; $x ^= $y; echo $x; //Got b echo $y; //Got a How does the operator ^ work here? 回答1: This looks like swapping a value using XOR. Though I am not sure about the strings in PHP (normally you use it for ints or something). For a truth table of XOR you can look here. The interesting thing about XOR is that it is reversable: A XOR B XOR B == A ... that is not working with AND or OR . Because of

How to set/unset a bit at specific position of a long?

扶醉桌前 提交于 2019-12-18 10:34:36
问题 How to set/unset a bit at specific position of a long in Java ? For example, long l = 0b001100L ; // bit representation I want to set bit at position 2 and unset bit at position 3 thus corresponding long will be, long l = 0b001010L ; // bit representation Can anybody help me how to do that ? 回答1: To set a bit, use: x |= 0b1; // set LSB bit x |= 0b10; // set 2nd bit from LSB to erase a bit use: x &= ~0b1; // unset LSB bit (if set) x &= ~0b10; // unset 2nd bit from LSB to toggle a bit use: x ^=

Understanding bitwise operations and their application in Java

一曲冷凌霜 提交于 2019-12-18 07:12:40
问题 I think understand what they fundamentally do - operate on bits (flip, shift, invert, etc...). My issue is that I don't know when I'd ever need to use them, and I don't think I fully understand bits. I know that there are 8 bits in a byte and I know that bits are either a 0 or 1 . Now here is where I start to get confused... I believe data types define combinations of bits differently. So if I declare an int , 32 bits are set aside for numbers, if I declare a char, 8 bits are set aside and

What does x >>> 0 do? [duplicate]

[亡魂溺海] 提交于 2019-12-17 23:46:48
问题 This question already exists : Closed 9 years ago . Possible Duplicate: What good does zero-fill bit-shifting by 0 do? (a >>> 0) I've been trying out some functional programming concepts in a project of mine and I was reading about Array.prototype.map , which is new in ES5 and looks like this: Array.prototype.map = function(fun) { "use strict"; if (this === void 0 || this === null) { throw new TypeError(); } var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") {

bitwise-ANDing with 0xff is important?

 ̄綄美尐妖づ 提交于 2019-12-17 23:39:39
问题 Doesn't bitwise-ANDing with 0xff essentially mean getting the same value back, for that matter, in this code? byte[] packet = reader.readPacket(); short sh; sh = packet[1]; sh &= 0xFF; System.out.print(sh+" "); Weirdly, I get a -1 if that ANDing is not included but a 255 when included Could someone explain the reason? As I see it 0xff is just 1111 1111. Isn't it? 回答1: Yes, 0xff is just 1111 1111 . But this is attempting to display the unsigned byte value, even though in Java byte s are signed