bit-manipulation

Bitwise xor 0xFFFFFFFF?

こ雲淡風輕ζ 提交于 2019-12-02 17:19:42
问题 I couldn't wrap my head around this: def expr(a): return ~(a ^ 0xFFFFFFFF), a ^ 0xFFFFFFFF, ~a, a print(expr(0xFFFFFFFF)) print(expr(1)) print(expr(0)) print(expr(-1)) I understand ~a means two's complement of a , but a ^ 0xFFFFFFFF also flips all the bits, but python will interpret it as a large number. I know Python3 is using unbound integer size, how does that work? Can someone ELI5 (Explain Like I'm Five)? Results: ( -1, 0, -4294967296, 4294967295) (-4294967295, 4294967294, -2, 1) (

PHP shift right

笑着哭i 提交于 2019-12-02 17:07:41
问题 i am trying to implement DataOutputStream in php (DataOutputStream from java language) in java code they shift right variables like this >>> in php i can only shift like this >> how can i do this in php ? thank you 回答1: You can implement the behavior of the unsigned right shift operator >>> with the signed shift operators like this: The value of n>>>s is n right-shifted s bit positions with zero-extension. If n is positive, then the result is the same as that of n>>s ; if n is negative, the

Why is squaring a number faster than multiplying two random numbers?

六眼飞鱼酱① 提交于 2019-12-02 17:07:40
Multiplying two binary numbers takes n^2 time, yet squaring a number can be done more efficiently somehow. (with n being the number of bits) How could that be? Or is it not possible? This is insanity! There exist algorithms more efficient than O(N^2) to multiply two numbers (see Karatsuba, Pollard, Schönhage–Strassen, etc.) The two problems "multiply two arbitrary N-bit numbers" and "Square an arbitrary N-bit number" have the same complexity. We have 4*x*y = (x+y)^2 - (x-y)^2 So if squaring N-bit integers takes O(f(N)) time, then the product of two arbitrary N-bit integers can be obtained in O

How to get the Nth digit of an integer with bit-wise operations?

落爺英雄遲暮 提交于 2019-12-02 16:15:23
Example. 123456, and we want the third from the right ('4') out. The idea in practise is to access each digit seperately (ie. 6 5 4 3 2 1). C/C++/C# preferred. A more efficient implementation might be something like this: char nthdigit(int x, int n) { while (n--) { x /= 10; } return (x % 10) + '0'; } This saves the effort of converting all digits to string format if you only want one of them. And, you don't have to allocate space for the converted string. If speed is a concern, you could precalculate an array of powers of 10 and use n to index into this array: char nthdigit(int x, int n) {

Extracting bits with bitwise operators [closed]

寵の児 提交于 2019-12-02 16:06:10
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I'm trying to learn how to use bitwise operators on a given input but am not having much luck figuring out how to use them. Let's say I have this following octet: 11(01)0000 How would I extract the bits between

Convert 0x1234 to 0x11223344

时光毁灭记忆、已成空白 提交于 2019-12-02 15:24:39
How do I expand the hexadecimal number 0x1234 to 0x11223344 in a high-performance way? unsigned int c = 0x1234, b; b = (c & 0xff) << 4 | c & 0xf | (c & 0xff0) << 8 | (c & 0xff00) << 12 | (c & 0xf000) << 16; printf("%p -> %p\n", c, b); Output: 0x1234 -> 0x11223344 I need this for color conversion. Users provide their data in the form 0xARGB, and I need to convert it to 0xAARRGGBB . And yes, there could be millions, because each could be a pixel. 1000x1000 pixels equals to one million. The actual case is even more complicated, because a single 32-bit value contains both foreground and background

Microsoft Interview: transforming a matrix

天涯浪子 提交于 2019-12-02 15:17:50
Given a matrix of size n x m filled with 0's and 1's e.g.: 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 if the matrix has 1 at (i,j), fill the column j and row i with 1's i.e., we get: 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 Required complexity: O(n*m) time and O(1) space NOTE: you are not allowed to store anything except '0' or '1' in the matrix entries Above is a Microsoft Interview Question. I thought for two hours now. I have some clues but can't proceed any more. Ok. The first important part of this question is that Even using a straight forward brute-force way , it can't be easily solved. If

Increment Table ID Field with Bitwise Counting

本秂侑毒 提交于 2019-12-02 14:44:42
问题 I am working on a project where I need my ID column to be a power of 2 ( 1,2,4,8,16.. ). I know that we cannot offset the auto_increment but for simple addition/subtraction in my.cnf . Example: id ---- 1 2 4 8 16 32 64 128 etc One of the ideas I had was to use the auto increment functionality as the base, and then create a trigger to apply the power of 2 and update the new ID, but unfortunately, it is not working: DELIMITER $$ CREATE TRIGGER testbitcompatid BEFORE INSERT ON Table FOR EACH ROW

Bit mask in C

人盡茶涼 提交于 2019-12-02 14:42:50
What is the best way to construct a bit mask in C with m set bits preceded by k unset bits, and followed by n unset bits: 00..0 11..1 00..0 k m n For example, k=1, m=4, n=3 would result in the bit mask: 01111000 ~(~0 << m) << n So, you are asking for m set bits prefixed by k reset bits and followed by n reset bits? We can ignore k since it will largely be constrained by the choice of integer type. mask = ((1 << m) - 1) << n; quinmars I like both solutions. Here is another way that comes to my mind (probably not better). ((~((unsigned int)0) << k) >> (k + n)) << n EDIT: There was a bug in my

Explain the following C++ method

我怕爱的太早我们不能终老 提交于 2019-12-02 14:01:11
问题 #define XL 33 #define OR 113 #define NOR 313 #define TN 344 int to_bits(int critn,char *mask) { unsigned int x; int begin; if (critn < XL) begin = 1; else if (critn < OR) begin = XL; else if (critn < NOR) begin = OR; else if (critn <= TN) begin = NOR; else begin = 0; x = critn - begin; *mask = (char)(0x80 >> (x % 8)); return (int)(x >> 3); // fast divide by 8 } I don't have any knowledge of C++ code. Can any one explain what this method is doing in the last 2 lines? Thanks 回答1: In C++, just