FreeBASIC  0.91.0
strw_bin_lng.c
Go to the documentation of this file.
1 /* binw$ routine for long long's */
2 
3 #include "fb.h"
4 
5 FBCALL FB_WCHAR *fb_WstrBinEx_l ( unsigned long long num, int digits )
6 {
7  FB_WCHAR *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_wstr_AllocTemp( digits );
26  if( s == NULL )
27  return NULL;
28 
29  i = digits - 1;
30  while( i >= 0 ) {
31  s[i] = _LC('0') + (num & 1); /* '0' or '1' */
32  num >>= 1;
33  i -= 1;
34  }
35 
36  s[digits] = _LC('\0');
37  return s;
38 }
39 
40 FBCALL FB_WCHAR *fb_WstrBin_l ( unsigned long long num )
41 {
42  return fb_WstrBinEx_l( num, 0 );
43 }