Is “-1>>5;” unspecified behavior in C?

Both are correct. Implementation defined behavior is a particular type of unspecified behavior. Citing section 3.4.1 of the C standard which defines “implementation-defined behavior”: 1 implementation-defined behavior unspecified behavior where each implementation documents how the choice is made 2 EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit when a signed … Read more

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

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 … Read more

How to find the position of the only-set-bit in a 64-bit value using bit manipulation efficiently?

Multiply the value by a carefully designed 64-bit constant, then mask off the upper 4 bits. For any CPU with fast 64-bit multiplication, this is probably as optimal as you can get. int field_set(uint64_t input) { uint64_t field = input * 0x20406080a0c0e1ULL; return (field >> 60) & 15; } // field_set(0x0000000000000000ULL) = 0 // field_set(0x0000000000000080ULL) … Read more

Bit count : preprocessor magic vs modern C++

Why not use the standard library? #include <bitset> int bits_in(std::uint64_t u) { auto bs = std::bitset<64>(u); return bs.count(); } resulting assembler (Compiled with -O2 -march=native): bits_in(unsigned long): xor eax, eax popcnt rax, rdi ret It is worth mentioning at this point that not all x86 processors have this instruction so (at least with gcc) you … Read more

bitwise AND in Javascript with a 64 bit integer

Javascript represents all numbers as 64-bit double precision IEEE 754 floating point numbers (see the ECMAscript spec, section 8.5.) All positive integers up to 2^53 can be encoded precisely. Larger integers get their least significant bits clipped. This leaves the question of how can you even represent a 64-bit integer in Javascript — the native … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)