File: binfmt.c

package info (click to toggle)
msort 8.53-2.2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,360 kB
  • sloc: sh: 10,138; ansic: 10,031; makefile: 51
file content (118 lines) | stat: -rw-r--r-- 2,728 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Time-stamp: <2009-12-25 21:20:47 poser> */
/*
 * Copyright (C) 2003, 2009 William J. Poser (billposer@alum.mit.edu)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 of the  GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * or go to the web page:  http://www.gnu.org/copyleft/lesser.html
 */

/*
 * These functions format unsigned integers into base-2 text.
 * They return a pointer to static storage containing the resulting
 * null terminated string.  There are five otherwise identical functions
 * for char,short,int, long, and long long operands. They are
 * written in such a way as to adapt at compile time to the
 * operand sizes used on a particular machine.
 *
 * They assume that a byte consists of eight bits. In the unlikely
 * event that this is not true of your system, define CHAR_BITS to the
 * appropriate value.
 */

#ifndef CHAR_BITS
#define CHAR_BITS 8
#endif

#define CBITS (sizeof(unsigned char) * CHAR_BITS)
#define SBITS (sizeof(unsigned short) * CHAR_BITS)
#define IBITS (sizeof(unsigned int) * CHAR_BITS)
#define LBITS (sizeof(unsigned long) * CHAR_BITS)
#define QBITS (sizeof(unsigned long long) * CHAR_BITS)

#define MASK 0x01

char *
binfmtc(unsigned char n)
{
  int i;
  static char str [CBITS+1];

  for (i = CBITS-1; i >= 0;i--){
    str[i] = (n & MASK ? '1' : '0');
    n >>= 1;
  }

  str[CBITS] = '\0';
  return(str);
}

char *
binfmts(unsigned short n)
{
  int i;
  static char str [SBITS+1];

  for (i = SBITS-1; i >= 0;i--){
    str[i] = (n & MASK ? '1' : '0');
    n >>= 1;
  }

  str[SBITS] = '\0';
  return(str);
}

char *
binfmti(unsigned int n)
{
  int i;
  static char str [IBITS+1];

  for (i = IBITS-1; i >= 0;i--){
    str[i] = (n & MASK ? '1' : '0');
    n >>= 1;
  }

  str[IBITS] = '\0';
  return(str);
}

char *
binfmtl(unsigned long n)
{
  int i;
  static char str [LBITS+1];

  for (i = LBITS-1; i >= 0;i--){
    str[i] = (n & MASK ? '1' : '0');
    n >>= 1;
  }

  str[LBITS] = '\0';
  return(str);
}

char *
binfmtq(unsigned long long n)
{
  int i;
  static char str [QBITS+1];

  for (i = QBITS-1; i >= 0;i--){
    str[i] = (n & MASK ? '1' : '0');
    n >>= 1;
  }

  str[QBITS] = '\0';
  return(str);
}