Names specified here
Name Description Notes Source Availability
signed Specifier for signed integer types L Q Keyword C89 C90 C95 C99 C11
unsigned Specifier for unsigned integer types L Q Keyword C89 C90 C95 C99 C11
Names specified for <limits.h>, “Integer type characteristics”
Name Description Notes Source Availability
CHAR_BIT Number of bits in unsigned char L M <limits.h> C89 C90 C95 C99 C11
CHAR_MAX Maximum value of char L M <limits.h> C89 C90 C95 C99 C11
CHAR_MIN Minimum value of char L M <limits.h> C89 C90 C95 C99 C11
INT_MAX Maximum value of int L M <limits.h> C89 C90 C95 C99 C11
INT_MIN Minimum value of int L M <limits.h> C89 C90 C95 C99 C11
LLONG_MAX Maximum value of long long L M <limits.h> C99 C11
LLONG_MIN Minimum value of long long L M <limits.h> C99 C11
LONG_MAX Maximum value of long L M <limits.h> C89 C90 C95 C99 C11
LONG_MIN Minimum value of long L M <limits.h> C89 C90 C95 C99 C11
MB_LEN_MAX Maximum multibyte character length L M <limits.h> C89 C90 C95 C99 C11
SCHAR_MAX Maximum value of signed char L M <limits.h> C89 C90 C95 C99 C11
SCHAR_MIN Minimum value of signed char L M <limits.h> C89 C90 C95 C99 C11
SHRT_MAX Maximum value of short L M <limits.h> C89 C90 C95 C99 C11
SHRT_MIN Minimum value of short L M <limits.h> C89 C90 C95 C99 C11
UCHAR_MAX Maximum value of unsigned char L M <limits.h> C89 C90 C95 C99 C11
UINT_MAX Maximum value of unsigned L M <limits.h> C89 C90 C95 C99 C11
ULLONG_MAX Maximum value of unsigned long long L M <limits.h> C99 C11
ULONG_MAX Maximum value of unsigned long L M <limits.h> C89 C90 C95 C99 C11
USHRT_MAX Maximum value of unsigned short L M <limits.h> C89 C90 C95 C99 C11

This header is available in C89, C90, C95, C99 and C11.

An unsigned integer type is capable of representing any member of a finite subset of non-negative integers from zero upto 2v−1. It is represented by v value bits, and may have padding bits (except for char and unsigned char). If v is known, the number of padding bits in type T must be CHAR_BIT * sizeof(T) - v. Mathematical operations on unsigned integers work modulo 2v.

The keyword unsigned is present in the names of most native unsigned integer types. The following types are guaranteed to be unsigned integer types:

A signed integer type is capable of representing any member of a finite subset of integers roughly centred around zero. Signed types may use two's complement, one's complement or sign-and-magnitude binary representations. A signed integer type consists of v value bits, one sign bit, and possibly padding bits (except for char and signed char).

To determine which representation is in use, perform the following tests (based on a StackOverflow question):

#include <limits.h>

if (-1 == ~0) {
  // two's complement
} else if (~0 == -INT_MAX) {
  // sign-and-magnitude
} else {
  // one's complement
}
[ Work in progress : Are these comparisons suitable for the preprocessor?]
[ Work in progress : Do all signed types use the same representation?]

The keyword signed is present in the longer names of most native signed integer types, but can often be omitted. The following types are guaranteed to be signed integer types:

Together with all enumeration types and char, the unsigned and signed integer types constitute the integer types. The operands of the following operators must have integer type:

shift-expression
shift-expression << additive-expression
shift-expression >> additive-expression
multiplicative-expression
multiplicative-expression % cast-expression
inclusive-OR-expression
inclusive-OR-expression | exclusive-OR-expression
exclusive-OR-expression
exclusive-OR-expression ^ AND-expression
AND-expression
AND-expression & equality-expression
assignment-expression
unary-expression assignment-operator assignment-expression
assignment-operator
<<=
>>=
&=
^=
|=

The controlling expression of a switch must be of integer type:

selection-statement
switch ( expression ) statement

Integer types are also an essential component of pointer arithmetic. The binary operators + and [] require one operand to be of integer type if the other is an object pointer. Similarly, += and -= require the second operand to be of integer type if the first is an object pointer. If the first operand of - is an object pointer but the second isn't, it must be integer.

Some correct and incorrect examples would be good here.

The types char and wchar_t could be signed or unsigned. Each type can be individually tested using CHAR_MIN and WCHAR_MIN respectively, which are zero for unsigned.

An integer type's precision is v (the number of value bits). Its width is also v if it is unsigned, or v+1 if it is signed.

<limits.h> defines a number of macros that describe the characteristics of native integer types.

The following types are guaranteed to be integer; some are variations or aliases of others, and other, non-standard types may exist:

Integer constants

Octal constants consist of digits 0 to 7, and begin with 0. Decimal constants consist of digits 0 to 9, but must not start with 0, or they will be mistaken for octals. Hexadecimal constants start with 0x or 0X, and use the digits 0 to 9, plus A to F and a to f. Integer constants are always non-negative, and do not include a sign.

constant
integer-constant
integer-constant
untyped-integer-constant integer-suffixopt
untyped-integer-constant
decimal-constant
octal-constant
hexadecimal-constant
decimal-constant
nonzero-digit
decimal-constant digit
octal-constant
0
octal-constant octal-digit
hexadecimal-constant
hexadecimal-prefix hexadecimal-digit
hexadecimal-constant hexadecimal-digit
hexadecimal-prefix
0x
0X
digit
any of 0 1 2 3 4 5 6 7 8 9
nonzero-digit
any of 1 2 3 4 5 6 7 8 9
octal-digit
any of 0 1 2 3 4 5 6 7
hexadecimal-digit
any of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

The integer suffix indicates the precise type of the constant. L indicates long or unsigned long. LL indicates long long or unsigned long long. U indicates unsigned, unsigned long or unsigned long long.

integer-suffix
unsigned-suffix long-suffixopt
unsigned-suffix long-long-suffix
long-suffix unsigned-suffixopt
long-long-suffix unsigned-suffixopt
unsigned-suffix
u
U
long-suffix
l
L
long-long-suffix
ll
LL

There are no constants for char, unsigned char, signed char, short or unsigned short. Just use a constant for the other types, and let it be truncated.

There is no built-in notation for integer constants expressed in binary. However, you can use macros to build one:

#include <stdio.h>
#include <inttypes.h>

// Mathew Hendry's macro for binary integer literals - C / C++

#define BIT(N, K) (((N >>(4*K)) & 1) << K)

#define HCB(N) \
  (BIT(N, 0) | BIT(N, 1) | BIT(N, 2) | BIT(N, 3) |      \
   BIT(N, 4) | BIT(N, 5) | BIT(N, 6) | BIT(N, 7))

#define BIN8(S) (HCB(0x ## S ## UL))
#define BIN32(A, B, C, D)                                               \
  ((HCB(0x ## A ## UL) << 24) |    (HCB(0x ## B ## UL) << 16) |    (HCB(0x ## C ## UL) << 8) |    (HCB(0x ## D ## UL)))

#define TEST8(S) printf("%8s -> %016jX\n", #S, (uintmax_t) BIN8(S))
#define TEST32(A,B,C,D) \
  printf("%8s%8s%8s%8s -> %016jX\n", #A, #B, #C, #D, \
         (uintmax_t) BIN32(A, B, C, D))

int main()
{
  TEST8(00100110);
  TEST32(1001,11,1100011,11011011);
  return 0;
}
00100110 -> 0000000000000026
    1001      11 110001111011011 -> 00000000090363DB

Bit counting

If you can obtain the maximum value for an unsigned integer type, you can put it through Hallvard B. Furuseth's bit-counting macro:

/* Hallvard B. Furuseth's bit-counting computation */
#define IMAX_BITS(m) \
((m) /((m)%0x3fffffffL+1) /0x3fffffffL \
%0x3fffffffL*30 + (m)%0x3fffffffL \
/((m)%31+1)/31%31*5 + 4-12/((m)%31+3))

It only works with all-bits-set values, i.e., for 2n−1, it yields n, but it is safe to use in preprocessor computations. Here's a program to establish that it works:

#include <stdio.h>
#include <inttypes.h>

/* Generate 2^n-1, without exceeding the shift range of the type. */
static uintmax_t generate(int n)
{
  uintmax_t r = UINTMAX_MAX;
  while (n > 0)
    r <<= 1, n--;
  return ~r;
}

int main(void)
{
  /* How many bits in our type? */
  const int maxwidth = IMAX_BITS(UINTMAX_MAX);

  /* How many hex chars does this require? */
  const int xwidth = (maxwidth + 3) / 4;

  int lastn = -1;

  /* Start with all-bits-set,
         and eliminate the MSB each time. */
  for (uintmax_t word = UINTMAX_MAX; word; word >>= 1) {
    /* How many set bits in this word? */
    int n = IMAX_BITS(word);

    /* Is it one less than the previous value? */
    const char *npass =
        lastn < 0 || n == lastn - 1 ? "PASS" : "FAIL";
    lastn = n;

    /* Try to regenerate the word from
            the number of bits. */
    uintmax_t generated = generate(n);

    const char *genpass =
        generated == word ? "PASS" : "FAIL";

    /* Visually confirm that they are the same. */
    printf("%0*jx %4d (%s) %0*jx (%s)\n",
           xwidth, word, n, npass, xwidth,
           generated, genpass);
  }
  if (lastn != 1)
    printf("FAILURE! Final count was not 1.\n");

  return 0;
}

The output should be something like this:

ffffffffffffffff   64 (PASS) ffffffffffffffff (PASS)
7fffffffffffffff   63 (PASS) 7fffffffffffffff (PASS)
3fffffffffffffff   62 (PASS) 3fffffffffffffff (PASS)
1fffffffffffffff   61 (PASS) 1fffffffffffffff (PASS)
0fffffffffffffff   60 (PASS) 0fffffffffffffff (PASS)
07ffffffffffffff   59 (PASS) 07ffffffffffffff (PASS)
03ffffffffffffff   58 (PASS) 03ffffffffffffff (PASS)
01ffffffffffffff   57 (PASS) 01ffffffffffffff (PASS)
00ffffffffffffff   56 (PASS) 00ffffffffffffff (PASS)
007fffffffffffff   55 (PASS) 007fffffffffffff (PASS)
003fffffffffffff   54 (PASS) 003fffffffffffff (PASS)
001fffffffffffff   53 (PASS) 001fffffffffffff (PASS)
000fffffffffffff   52 (PASS) 000fffffffffffff (PASS)
0007ffffffffffff   51 (PASS) 0007ffffffffffff (PASS)
0003ffffffffffff   50 (PASS) 0003ffffffffffff (PASS)
0001ffffffffffff   49 (PASS) 0001ffffffffffff (PASS)
0000ffffffffffff   48 (PASS) 0000ffffffffffff (PASS)
00007fffffffffff   47 (PASS) 00007fffffffffff (PASS)
00003fffffffffff   46 (PASS) 00003fffffffffff (PASS)
00001fffffffffff   45 (PASS) 00001fffffffffff (PASS)
00000fffffffffff   44 (PASS) 00000fffffffffff (PASS)
000007ffffffffff   43 (PASS) 000007ffffffffff (PASS)
000003ffffffffff   42 (PASS) 000003ffffffffff (PASS)
000001ffffffffff   41 (PASS) 000001ffffffffff (PASS)
000000ffffffffff   40 (PASS) 000000ffffffffff (PASS)
0000007fffffffff   39 (PASS) 0000007fffffffff (PASS)
0000003fffffffff   38 (PASS) 0000003fffffffff (PASS)
0000001fffffffff   37 (PASS) 0000001fffffffff (PASS)
0000000fffffffff   36 (PASS) 0000000fffffffff (PASS)
00000007ffffffff   35 (PASS) 00000007ffffffff (PASS)
00000003ffffffff   34 (PASS) 00000003ffffffff (PASS)
00000001ffffffff   33 (PASS) 00000001ffffffff (PASS)
00000000ffffffff   32 (PASS) 00000000ffffffff (PASS)
000000007fffffff   31 (PASS) 000000007fffffff (PASS)
000000003fffffff   30 (PASS) 000000003fffffff (PASS)
000000001fffffff   29 (PASS) 000000001fffffff (PASS)
000000000fffffff   28 (PASS) 000000000fffffff (PASS)
0000000007ffffff   27 (PASS) 0000000007ffffff (PASS)
0000000003ffffff   26 (PASS) 0000000003ffffff (PASS)
0000000001ffffff   25 (PASS) 0000000001ffffff (PASS)
0000000000ffffff   24 (PASS) 0000000000ffffff (PASS)
00000000007fffff   23 (PASS) 00000000007fffff (PASS)
00000000003fffff   22 (PASS) 00000000003fffff (PASS)
00000000001fffff   21 (PASS) 00000000001fffff (PASS)
00000000000fffff   20 (PASS) 00000000000fffff (PASS)
000000000007ffff   19 (PASS) 000000000007ffff (PASS)
000000000003ffff   18 (PASS) 000000000003ffff (PASS)
000000000001ffff   17 (PASS) 000000000001ffff (PASS)
000000000000ffff   16 (PASS) 000000000000ffff (PASS)
0000000000007fff   15 (PASS) 0000000000007fff (PASS)
0000000000003fff   14 (PASS) 0000000000003fff (PASS)
0000000000001fff   13 (PASS) 0000000000001fff (PASS)
0000000000000fff   12 (PASS) 0000000000000fff (PASS)
00000000000007ff   11 (PASS) 00000000000007ff (PASS)
00000000000003ff   10 (PASS) 00000000000003ff (PASS)
00000000000001ff    9 (PASS) 00000000000001ff (PASS)
00000000000000ff    8 (PASS) 00000000000000ff (PASS)
000000000000007f    7 (PASS) 000000000000007f (PASS)
000000000000003f    6 (PASS) 000000000000003f (PASS)
000000000000001f    5 (PASS) 000000000000001f (PASS)
000000000000000f    4 (PASS) 000000000000000f (PASS)
0000000000000007    3 (PASS) 0000000000000007 (PASS)
0000000000000003    2 (PASS) 0000000000000003 (PASS)
0000000000000001    1 (PASS) 0000000000000001 (PASS)

The computation is supposed to have a limit of about 3·2×1010. Consider the following test program, which exploits BigInteger, Java's built-in arbitrary-precision integer type, if you have any doubts about its range:

import java.math.BigInteger;

public class FurusethBitCount {
  public static void main(String args) {
    BigInteger v = BigInteger.ONE;
    BigInteger unit = v;
    int n = 1;

    for ( ; ; ) {
      int x = countBits(v);
      System.out.printf("%d -> %x -> %d%n", n, v, x);
      if (x != n) break;
      v = v.shiftLeft(1).or(unit);
      n++;
    }
  }

  private static final BigInteger I1 = BigInteger.ONE;

  private static final BigInteger I3 = i(3);
  private static final BigInteger I4 = i(4);
  private static final BigInteger I5 = i(5);
  private static final BigInteger I12 = i(12);
  private static final BigInteger I30 = i(30);
  private static final BigInteger I31 = i(31);

  private static final BigInteger X3fffffff =
      new BigInteger("3fffffff",16);

  private static BigInteger i(int i) {
    return new BigInteger(Integer.toString(i));
  }

  private static int countBits(BigInteger v) {
    BigInteger term1 = v
      .divide(v.mod(X3fffffff)
              .add(I1))
      .divide(X3fffffff)
      .mod(X3fffffff)
      .multiply(I30);
    BigInteger term2 = v
      .mod(X3fffffff)
      .divide(v.mod(I31).add(I1))
      .divide(I31)
      .mod(I31)
      .multiply(I5);
    BigInteger term3 = I4;
    BigInteger term4 = I12.divide(v.mod(I31).add(I3));

    return term1.add(term2).add(term3)
      .subtract(term4).intValue();
  }

  private FurusethBitCount() { }
}

CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2023-10-04T20:24:03.213+0000