bit-manipulation

Determining a string has all unique characters without using additional data structures and without the lowercase characters assumption

只愿长相守 提交于 2019-11-30 15:14:04
问题 This is one of the questions in the Cracking the Coding Interview book by Gayle Laakmann McDowell: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? The author wrote: We can reduce our space usage a little bit by using a bit vector. We will assume, in the below code, that the string is only lower case 'a' through 'z' . This will allow us to use just a single int. The author has this implementation: public static

bitwise operator and single ampersand [duplicate]

一笑奈何 提交于 2019-11-30 14:43:20
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why do we usually use || not |, what is the difference? Can I use single ampersand like & instead of a bitwise operator like && ? What kind of differences may arise and is there a specific example to illustrate this issue clearly? 回答1: The single & will check both conditions always. The double && will stop after the first condition if it evaluates to false. Using two is a way of "short circuiting" the condition

Why we need to add 1 while doing 2's complement

你离开我真会死。 提交于 2019-11-30 14:25:22
问题 The 2's complement of a number which is represented by N bits is 2^N-number. For example: if number is 7 (0111) and i'm representing it using 4 bits then, 2's complement of it would be (2^N-number) i.e. (2^4 -7)=9(1001) 7==> 0111 1's compliment of 7==> 1000 1000 + 1 ------------- 1001 =====> (9) While calculating 2's complement of a number, we do following steps: 1. do one's complement of the number 2. Add one to the result of step 1. I understand that we need to do one's complement of the

How do you set, clear and toggle a single bit in Rust?

柔情痞子 提交于 2019-11-30 14:12:18
问题 How do I set, clear and toggle a bit in Rust? 回答1: Like many other languages, the bitwise operators & (bitwise AND), | (bitwise OR), ^ (bitwise XOR) exist: fn main() { let mut byte: u8 = 0b0000_0000; byte |= 0b0000_1000; // Set a bit println!("0b{:08b}", byte); byte &= 0b1111_0111; // Unset a bit println!("0b{:08b}", byte); byte ^= 0b0000_1000; // Toggle a bit println!("0b{:08b}", byte); } The main difference from other languages is in bitwise NOT, which uses ! instead of ~ : fn main() { let

bit shift operation does not return expected result

十年热恋 提交于 2019-11-30 14:11:22
问题 Why does Java return -2147483648 when I bit shift 1 << 63 ? The expected result is 9 223 372 036 854 775 808 , tested with Wolfram Alpha and my calculator. I tested: System.out.print((long)(1 << (63))); 回答1: There's an important thing to note about the line System.out.print((long)(1 << (63))); You first take (1 << 63) , and then you cast to long. As a result, you are actually left-shifting in integers, so the long cast doesn't have any effect. That's why shifting 63 bits left gives the min

Bit reversal of an integer, ignoring integer size and endianness

Deadly 提交于 2019-11-30 14:11:17
Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I have the following code to reverse the bits of an integer: TYPE max_bit= (TYPE)-1; void reverse_int_setup() { TYPE bits= (TYPE)max_bit; while (bits <<= 1) max_bit= bits; } TYPE reverse_int(TYPE arg) { TYPE bit_setter= 1, bit_tester= max_bit, result= 0; for (result= 0; bit_tester; bit_tester>>= 1, bit_setter<<= 1) if (arg & bit_tester) result|= bit_setter; return result; } One just needs first to run reverse_int_setup(), which stores an integer with the highest bit turned on, then any call to reverse_int( arg

Determining a string has all unique characters without using additional data structures and without the lowercase characters assumption

你离开我真会死。 提交于 2019-11-30 14:06:01
This is one of the questions in the Cracking the Coding Interview book by Gayle Laakmann McDowell: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? The author wrote: We can reduce our space usage a little bit by using a bit vector. We will assume, in the below code, that the string is only lower case 'a' through 'z' . This will allow us to use just a single int. The author has this implementation: public static boolean isUniqueChars(String str) { int checker = 0; for (int i = 0; i < str.length(); ++i) { int val = str

How do uppercase and lowercase letters differ by only one bit?

筅森魡賤 提交于 2019-11-30 13:49:57
问题 I have found one example in Data and Communication Networking book written by Behrouza Forouzan regarding upper- and lowercase letters which differ by only one bit in the 7 bit code. For example, character A is 1000001 (0x41) and character a is 1100001 (0x61).The difference is in bit 6, which is 0 in uppercase letters and 1 in lowercase letters. If we know the code for one case, we can easily find the code for the other by adding or subtracting 32 in decimal, or we can just flip the sixth bit

how to split 64bit integer to two 32bit integers

梦想与她 提交于 2019-11-30 13:42:26
I want to split a 64bit integer into two 32bit integers: var bigInt = 0xffffff; var highInt = bigInt >> 8 // get the high bits 0xfff var lowInt = bigInt // cut of the first part (with &)? console.log(highInt); // 0xfff console.log(lowInt); // 0xfff // set them together again var reBigInt = (highInt << 8) + lowInt; Unfortunately neither getting the highInt nor getting the lowInt works... Could somebody give me the answer how I need to use the bitwise operators? regards Alexander Gessler EDIT JavaScript represents integers using IEEE double precision format , so there is no way to store

What is the value of ~0 in C?

为君一笑 提交于 2019-11-30 13:06:37
I want to get the values of INT_MIN and INT_MAX . I've tried ~0 and ~0 >> 1 since the leftmost bit is a sign bit but I got -1 for both of them. It's so confused that why ~0 doesn't turn out to be 0xffffffff and ~0 >> 1 to be 0x7fffffff ? Use: ~0U >> 1 Suffix 'U' for unsigned shift behavior. so, confused that why not ~0 turns out to be 0xffffffff ? See, what is 0 say in four bytes representation: BIT NUMBER 31 0 ▼ ▼ number bits 0000 0000 0000 0000 0000 0000 0000 0000 ▲ ▲ MSB LSB LSB - Least Significant Bit (numbered 0) MSB - Most Significant Bit (numbered 31) Now ~ is bitwise not operator then