bit-manipulation

bit manipulation: clearing range of bits

☆樱花仙子☆ 提交于 2019-12-18 11:55:11
问题 I'm preparing for an interview using the text, "Cracking the Coding Interview" by Gayle Laakman McDowell. On the section covering bit manipulation, there are two functions that are provided, but I don't quite understand how it works. // To clear all bits from the most significant bit through i (inclusive), we do: int clearMSBthroughI(int num, int i) { int mask = (1 << i) - 1; return num & mask; } // To clear all bits from i through 0 (inclusive), we do: int clearBitsIthrough0(int num, int i)

Bit Twiddling Hacks: interleave bits the obvious way [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 11:48:36
问题 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 9 years ago . i am interested on this problem Interleave bits the obvious way (from http://graphics.stanford.edu/~seander/bithacks.html) unsigned short x; // Interleave bits of x and y, so that all of the unsigned short y; //

Can XOR of two integers go out of bounds?

北慕城南 提交于 2019-12-18 11:38:23
问题 I had been studying the algorithm for finding lonely integers in an array, and here is the implementation: int arr[] = {10, 20, 30, 5, 20, 10, 30}; int LonelyInteger = 0; for(int i=0; i< 7; i++) { LonelyInteger = LonelyInteger ^ arr[i]; } The result is 5 . My question is - supposedly the integers (getting generated by the XOR operation) are too large due to this operation: LonelyInteger ^ arr[i] Which leads to a potentially large integer which cannot be represented by the datatype say int in

Getting the number of trailing 1 bits

放肆的年华 提交于 2019-12-18 11:23:53
问题 Are there any efficient bitwise operations I can do to get the number of set bits that an integer ends with? For example 11 10 = 1011 2 would be two trailing 1 bits. 8 10 = 1000 2 would be 0 trailing 1 bits. Is there a better algorithm for this than a linear search? I'm implementing a randomized skip list and using random numbers to determine the maximum level of an element when inserting it. I am dealing with 32 bit integers in C++. Edit: assembler is out of the question, I'm interested in a

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

落爺英雄遲暮 提交于 2019-12-18 11:06:08
问题 Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned , which have suffixes l and ll ): — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position. If x is 0, the result is undefined. — Built-in Function: int __builtin_ctz (unsigned int x) Returns the number of

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 11:05:49
问题 Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned , which have suffixes l and ll ): — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position. If x is 0, the result is undefined. — Built-in Function: int __builtin_ctz (unsigned int x) Returns the number of

Please explain the logic behind Kernighan's bit counting algorithm

泪湿孤枕 提交于 2019-12-18 10:53:20
问题 This question directly follows after reading through Bits counting algorithm (Brian Kernighan) in an integer time complexity . The Java code in question is int count_set_bits(int n) { int count = 0; while(n != 0) { n &= (n-1); count++; } } I want to understand what n &= (n-1) is achieving here ? I have seen a similar kind of construct in another nifty algorithm for detecting whether a number is a power of 2 like: if(n & (n-1) == 0) { System.out.println("The number is a power of 2"); } 回答1:

Two's Complement Binary in Python?

风流意气都作罢 提交于 2019-12-18 10:39:19
问题 Integers in Python are stored in two's complement, correct? Although: >>> x = 5 >>> bin(x) 0b101 And: >>> x = -5 >>> bin(x) -0b101 That's pretty lame. How do I get python to give me the numbers in REAL binary bits, and without the 0b infront of it? So: >>> x = 5 >>> bin(x) 0101 >>> y = -5 >>> bin(y) 1011 回答1: Not sure how to get what you want using the standard lib. There are a handful of scripts and packages out there that will do the conversion for you. I just wanted to note the "why" , and

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 ^=

Do bitwise operators (other than shifts) make any mathematical sense in base-10?

拥有回忆 提交于 2019-12-18 10:22:56
问题 According to wiki shifts can be used to calculate powers of 2: A left arithmetic shift by n is equivalent to multiplying by 2^n (provided the value does not overflow), while a right arithmetic shift by n of a two's complement value is equivalent to dividing by 2^n and rounding toward negative infinity. I was always wondering if any other bitwise operators ( ~ , | , & , ^ ) make any mathematical sense when applied to base-10? I understand how they work, but do results of such operations can be