[英]How can I use the MYSQL BIT_COUNT function through nhibernate/fluent nHibernate?
[英]How to simulate the MySQL bit_count function in Sybase SQL Anywhere?
MySQL的bit_count函數在某些情況下非常有用:
http://dev.mysql.com/doc/refman/5.5/zh-CN/bit-functions.html#function_bit-count
現在,我想在不支持該功能的其他數據庫中使用該功能。 什么是最簡單的方法(無需創建存儲函數,因為我無法在DDL級別上訪問客戶端數據庫)。
一個非常詳細的選項是(對於TINYINT
數據類型):
SELECT (my_field & 1) +
(my_field & 2) >> 1 +
(my_field & 4) >> 2 +
(my_field & 8) >> 3 +
(my_field & 16) >> 4 +
...
(my_field & 128) >> 7
FROM my_table
在Sybase SQL的情況下Anywhere中, >>
運營商似乎並不可用,所以由一個部門2, 4, 8, 16
作品也是如此。
還有其他更簡單的選擇嗎?
我發現這種算法在Java的Integer
和Long
類中較為冗長。 我完全不知道為什么它應該這樣工作:
public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.