swap

Why swap with xor works fine in c++ but in java doesn't ? some puzzle [duplicate]

余生长醉 提交于 2019-12-05 22:14:00
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Why is this statement not working in java x ^= y ^= x ^= y; Sample code int a=3; int b=4; a^=(b^=(a^=b)); In c++ it swaps variables, but in java we get a=0, b=4 why? 回答1: By writing your swap all in one statement, you are relying on side effects of the inner a^=b expression relative to the outer a^=(...) expression. Your Java and C++ compilers are doing things differently. In order to do the xor swap properly,

Swap button for image (Jquery)

无人久伴 提交于 2019-12-05 17:43:36
I have a button and when it's clicked, I want to replace the button with an image. How can I do this in JQuery? Is it possible to replace the background of the image as well? The button itself is within a large div, and I don't want to add another div around the button because it messes up a previous layout. If you want to replace the button element: $('the-button').bind('click', function () { $(this).replaceWith('<img src="/wherever.jpg"/>'); }); If you want to change the background image of the button: $('the-button').bind('click', function () { $(this).css('backgroundImage', 'url(\'

Command-line to reverse byte order/change endianess

回眸只為那壹抹淺笑 提交于 2019-12-05 14:51:24
问题 I'm hacking around in some scripts trying to parse some data written by Javas DataOutputStream#writeLong(...) . Since java always seems to write big endian, I have a problem feeding the bytes to od . This is due to the fact that od always assumes that the endianess matches the endianess of the arch that you are currently on, and I'm on a little endian machine. I'm looking for an easy one-liner to reverse the byte order. Let's say that you know that the last 8 bytes of a file is a long written

Variable swap with and without auxiliary variable - which is faster?

白昼怎懂夜的黑 提交于 2019-12-05 10:08:05
I guess you all heard of the 'swap problem'; SO is full of questions about it. The version of the swap without use of a third variable is often considered to be faster since, well, you have one variable less. I wanted to know what was going on behind the curtains and wrote the following two programs: int main () { int a = 9; int b = 5; int swap; swap = a; a = b; b = swap; return 0; } and the version without third variable: int main () { int a = 9; int b = 5; a ^= b; b ^= a; a ^= b; return 0; } I generated the assembly code using clang and got this for the first version (that uses a third

Swap two elements in jQuery

浪尽此生 提交于 2019-12-05 08:26:12
I'm trying to swap two elements using up and down arrows. A JSFiddle solution would be great! My HTML: <div class="item"> <div class="content">Some text</div> <div class="move"> <div class="move-down">down</div> </div> </div> <div class="item"> <div class="content">Some other text</div> <div class="move"> <div class="move-up">up</div> <div class="move-down">down</div> </div> </div> <div class="item"> <div class="content">Some text</div> <div class="move"> <div class="move-up">up</div> <div class="move-down">down</div> </div> </div> <div class="item"> <div class="content">Some other text</div>

Swapping two structures in c

て烟熏妆下的殇ゞ 提交于 2019-12-05 07:54:49
问题 Hi i'm trying to create a swap function that swaps the first two elements of the structure. Can someone please show me how to make this work. void swap(struct StudentRecord *A, struct StudentRecord *B){ struct StudentRecord *temp = *A; *A = *B; *B = *temp; } struct StudentRecord *pSRecord[numrecords]; for(int i = 0; i < numrecords; i++) { pSRecord[i] = &SRecords[i]; } printf("%p \n", pSRecord[0]); printf("%p \n", pSRecord[1]); swap(&pSRecord[0], &pSRecord[1]); printf("%p \n", pSRecord[0]);

How to effectively swap OpenCL memory buffers?

隐身守侯 提交于 2019-12-05 06:49:44
Exactly as the title suggests I am looking for how to effectively swap two OpenCL buffers. My kernel uses two gloabl buffers, one as input and one as output. However, I invoke my kernel in a for loop with the same NDRange, each time setting the kernel arguments, enqueueing the kernel, and swapping the buffers because the previous output buffer will be the input buffer seed for the next iteration. What is the appropriate way here, to swap these two buffers? I imagine that copying the buffer back to the host to one of the already malloc'd arrays and copying it into the next input buffer using

Swap method with const members

情到浓时终转凉″ 提交于 2019-12-05 05:03:04
I want to implement a Swap() method for my class (let's call it A) to make copy-and-swap operator=(). As far as I know, swap method should be implemented by swapping all members of the class, for example: class A { public: void swap(A& rhv) { std::swap(x, rhv.x); std::swap(y, rhv.y); std::swap(z, rhv.z); } private: int x,y,z; }; But what should I do if I have a const member? I can't call std::swap for it, so I can't code A::Swap(). EDIT: Actually my class is little bit more complicated. I want to Serialize and Deserialize it. Const member is a piece of data that won't change (its ID for

std: container c++ move to front

断了今生、忘了曾经 提交于 2019-12-05 01:24:58
I'm looking for a std container like a std::list that can efficiently move an element to the front: a-b-c-d-e move "b" to front: a-c-d-e-b There is no such function in the std containers. Therefor, I think I must combine a remove and push_front function but has anyone can find a better idea? Thank in advance. If you don't have to maintain the order of the other elements, then the simplest solution is doubtlessly just to swap the element you want with the first element in the container. This will be efficient with all containers. Otherwise, std::list offers a splice operation which could be

Why are the swap member functions in STL containers not declared noexcept?

狂风中的少年 提交于 2019-12-04 23:42:44
As of N3797 the C++ standard requires swap functions of containers to not throw any exceptions unless specified otherwise [container.requirements.general] ( 23.2.1§10 ). Why are the swap member functions that are specified to not throw not declared noexcept ? The same question applies to the specialized non-member swap overloads. Lightness Races with Monica Further to what refp said , here's a post from Daniel Krügler on the std-discussion mailing list: The internal policy to declare a function as unconditional noexcept is explained in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011