FreeBASIC  0.91.0
str_bin_lng.c
Go to the documentation of this file.
1 /* bin$ routine for long long's */
2 
3 #include "fb.h"
4 
5 FBCALL FBSTRING *fb_BINEx_l ( unsigned long long num, int digits )
6 {
7  FBSTRING *s;
8  int i;
9  unsigned long long num2;
10 
11  if( digits <= 0 ) {
12  /* Only use the minimum amount of digits needed; need to count
13  the important bits in the number. And if there are none set,
14  use 1 digit for 1 zero. */
15  digits = 0;
16  num2 = num;
17  while( num2 ) {
18  digits += 1;
19  num2 >>= 1;
20  }
21  if( digits == 0 )
22  digits = 1;
23  }
24 
25  s = fb_hStrAllocTemp( NULL, digits );
26  if( s == NULL )
27  return &__fb_ctx.null_desc;
28 
29  i = digits - 1;
30  while( i >= 0 ) {
31  s->data[i] = '0' + (num & 1); /* '0' or '1' */
32  num >>= 1;
33  i -= 1;
34  }
35 
36  s->data[digits] = '\0';
37  return s;
38 }
39 
40 FBCALL FBSTRING *fb_BIN_l ( unsigned long long num )
41 {
42  return fb_BINEx_l( num, 0 );
43 }