bitwise-operators

Convert IPv6 to binary without INET6_ATON()

£可爱£侵袭症+ 提交于 2019-12-06 07:46:12
Description: I am creating an IP class that first finds the visitor's IP address (using $_SERVER['REMOTE_ADDR'] or getenv('REMOTE_ADDR') ), makes sure it's a valid IP, set's the version (IPv4 or IPv6) to a data member, then it either returns the IP address, or returns the SQL parameter if it's for an SQL query (see createQueryParam() function below for available returns). Question: If the server is not running MySQL 5.6.3+, how can I convert a IPv6 binary stored in the database table ( varbinary(16) ) to a string? For an IPv6 and on systems not running MySQL 5.6.3+ I'd like to return a MySQL

Bitwise operations in Python

给你一囗甜甜゛ 提交于 2019-12-06 07:15:42
问题 I'm looking for recommendations on how to do bitwise math in python. The main problem I have is that python's bitwise operators have infinite precision, which means that -1 is really "111.......111". That's not what I want. I want to emulate real hardware which will have some fixed precision, say 32 bits. Here are some gotchas: 1) -n should return a 32 bit 2's complement number ( this is easily achieved by taking the lower 32 bits of the infinite precision -n ) 2) n >> 3, should be an

MySQL doesn't use indexes when query over BIT field using bitwise functions

喜夏-厌秋 提交于 2019-12-06 05:11:11
问题 I have a field of type BIT in my MySQL table. I want to store statuses of the record using bit value, for example: 1 = status1 2 = status2 4 = status3 8 = status4 Each record can have many statuses at once. For status1 and status3 the value will be 1 + 4 = 5. I can query table for all records with status3 using: SELECT * FROM `table` WHERE `statuses` & 4 I have index on statuses , but EXPLAIN tells that no index is used. Can I use index in such situation? P.S. Using separate many-to-many

difference between JavaScript bit-wise operator code and Python bit-wise operator code

大兔子大兔子 提交于 2019-12-06 03:54:02
I have converted JavaScript code which uses bit-wise operators in that code to Python code, but there is one problem when i do this in JavaScript and Python 412287 << 10 then I get this 422181888 same results in both languages. but when i do this in both 424970184 << 10 then i get different results in both of the languages 1377771520 in JavaScript and 435169468416 in Python can anybody help me with this? any help would be appreciated. If you want the java-script equivalent value then what you can do is : import ctypes print(ctypes.c_int(424970184 << 10 ^ 0).value) Output: 1377771520 Jean

The best way to shift a __m128i?

a 夏天 提交于 2019-12-06 02:40:18
问题 I need to shift a __m128i variable, (say v), by m bits, in such a way that bits move through all of the variable (So, the resulting variable represents v*2^m). What is the best way to do this?! Note that _mm_slli_epi64 shifts v0 and v1 seperately: r0 := v0 << count r1 := v1 << count so the last bits of v0 missed, but I want to move those bits to r1. Edit: I looking for a code, faster than this (m<64): r0 = v0 << m; r1 = v0 >> (64-m); r1 ^= v1 << m; r2 = v1 >> (64-m); 回答1: For compile-time

Get raw bytes of a float in Swift

馋奶兔 提交于 2019-12-06 01:51:49
问题 How can I read the raw bytes of a Float or Double in Swift? Example: let x = Float(1.5) let bytes1: UInt32 = getRawBytes(x) let bytes2: UInt32 = 0b00111111110000000000000000000000 I want bytes1 and bytes2 to contain the same value, since this binary number is the Float representation of 1.5 . I need it to do bit-wise operations like & and >> (these are not defined on a float). 回答1: Update for Swift 3: As of Swift 3, all floating point types have bitPattern property which returns an unsigned

Reverse bytes order of long

六眼飞鱼酱① 提交于 2019-12-05 21:35:11
I've got a long variable and I need to reverse its byte order. For example: B1, B2, ... , B8 I should return a long that consists of B8, B7, ..., B1 . How can I do it by using bitwise operations? Serdalis you can use Long.reverseBytes(long) Or for more methods which include bitwise operations, you can refer to this stack overflow question Heres another method you may like, I'd still recommend the above but it's better than bitwise where you can easily make mistakes. Bytebuffer byte[] bytes = ByteBuffer.allocate(8).putLong(someLong).array(); for (int left = 0, right = bytes.length - 1; left <

unsigned right shift '>>>' Operator in sql server [closed]

夙愿已清 提交于 2019-12-05 18:55:21
How to write unsigned right shift operator in sql server? The expression is like value >>> 0 Here is the e.g. -5381>>>0 = 4294961915 Ed B T-SQL has no bit-shift operators, so you'd have to implement one yourself. There's an implementation of a bitwise shifts here: http://dataeducation.com/bitmask-handling-part-4-left-shift-and-right-shift/ You'd have to cast your integer to a varbinary, use the bitwise shift function and cast back to integer and (hopefully) hey-presto! There's your result you're expecting. Implementation and testing is left as an exercise for the reader... Edit - To try to

Bitwise operation on a floating point usefulness

拜拜、爱过 提交于 2019-12-05 13:03:06
I noticed a SSE instruction existed for floating point AND which got me wondering. You can do the same thing with scalars in fp/integer union. The idea struck me that, if you bitwise OR the components of an array of floats, you can determine if any of them are negative quickly by looking at the sign bit of the result. What other uses exist for bitwise operations on floating point values? A lot. For example when you only need to do bitwise operations on a floating-point only instruction set like AVX, then those become very handy. Another application: making constants. You can see a lot of

Why doesn't left bit shifting by 64 overflow in golang?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 10:54:57
I was taking a look at A Tour of Go and I was confused by something in their basic-types.go example: MaxInt uint64 = 1<<64 - 1 Shouldn't shifting a 1 64 positions to the left in an unsigned 64 bit integer cause overflow (a.k.a. shifting a bit past the MSB)? However, the compiler doesn't complain until the line is changed to: MaxInt uint64 = 1<<65 - 1 ./basic-types.go:5: constant 36893488147419103231 overflows uint64 If I write some code to iterate over left shifts of varying lengths, including shifting by 65 as in the above example that causes the compiler to barf, I see two things: It behaves