开发者

How to get bitstring length of integer in oracle pl/sql?

开发者 https://www.devze.com 2023-02-08 10:52 出处:网络
How I can calculate bit length of an integer in Oracle\'s PL/SQL? I would like to get something like cast INT to BIT ST开发者_运维技巧RING and then LENGTH( LTRIM(BIT STRING, \'0\') )\'you can use the

How I can calculate bit length of an integer in Oracle's PL/SQL?

I would like to get something like cast INT to BIT ST开发者_运维技巧RING and then LENGTH( LTRIM(BIT STRING, '0') )'


you can use the following formula to get the number of characters of the binary representation of an integer n (n>0):

ceil(log(2, n + 1))

SQL> SELECT n, ceil(log(2, n + 1)) num_of_char
  2    FROM (SELECT ROWNUM n FROM dual CONNECT BY LEVEL <= 64);

         N NUM_OF_CHAR
---------- -----------
         1           1
         2           2
         3           2
         4           3
         5           3
         6           3
         7           3
         8           4
        [...]
        15           4
        16           5
        [...]
        31           5
        32           6
        [...]
        63           6
        64           7
0

精彩评论

暂无评论...
验证码 换一张
取 消