-1

I have the following for loop in C. I want to know why the Word Size is 64 bit rather than 32 bits.

#define WSIZE 8*sizeof(int)
long pcount_for(unsigned long x)
{
   size_t = i; 
   long result = 0;
   for (i = 0; i < WSIZE; i++)
   {
      unsigned bit = (x >> i) & 0x1;
      result += bit;
   }
   return result;
}
RR84
  • 111
  • 6
  • 3
    Because `sizeof(int)` is `8` on your system? – Ken Wayne VanderLinde Nov 15 '16 at 23:29
  • What is the platform and compiler used? – chux - Reinstate Monica Nov 15 '16 at 23:34
  • I'm on a 64 bit system. I thought INT size is 4 bytes? Platform is a 64 bit x-86/64. GCC Compiler. – RR84 Nov 15 '16 at 23:34
  • 2
    `int` is at least 16 bits long, but it may be longer. There's no upper bound. 32 bits is common, but by no means required. – Cornstalks Nov 15 '16 at 23:35
  • 1
    Should be using `#define WSIZE (CHAR_BIT *sizeof(int))` - likely same answer though – chux - Reinstate Monica Nov 15 '16 at 23:35
  • @RR84: on Linux, Windows and OS X running on x86_64 `int` is 4 bytes; what platform are you compiling for? – Matteo Italia Nov 15 '16 at 23:36
  • What OS are you using? – chux - Reinstate Monica Nov 15 '16 at 23:36
  • If you want a 32-bit integer, use `int32_t`. End of story. – paddy Nov 15 '16 at 23:36
  • Why is `WSIZE` using `sizeof(int)`, yet `x` is `unsigned long`? Is this true code or do you really have `#define WSIZE 8*sizeof(unsigned long)` or `#define WSIZE 8*sizeof(x)`? – chux - Reinstate Monica Nov 15 '16 at 23:38
  • Try just a printf("%d\n", sizeof(int)); Does it not print 4? – torhu Nov 15 '16 at 23:41
  • This is a piece of example code from a school lecture, I didn't write it. – RR84 Nov 15 '16 at 23:51
  • "example code from a school lecture" --> Did you compile and run it? or is the result only reported to you and not observed? – chux - Reinstate Monica Nov 15 '16 at 23:52
  • 2
    Result is reported to me, I ran it using a test function and did printf("%d\n", sizeof(int)); within my function. It's still showing the size of int as 4 bytes – RR84 Nov 16 '16 at 00:00
  • It's reasonable to say that the word size is 64 bits on x86_64, but there are different definitions in use. Register size is 64 bits when the CPU is running is 64 bit mode, I think that is usually what is meant by word size. But for those CPU's is complicated because of their history going back to the 16-bit era. A C int is always 32 bits on x86_64 as far as I know. – torhu Nov 16 '16 at 00:06
  • `long` is a 64-bit type in the LP64 data model. Using `long` to count the number of bits in an unsigned long is not very sensible, `int` is going to last for a very long time to come. – Hans Passant Nov 16 '16 at 00:45
  • @torhu the correct way is `printf("%zu", sizeof(int))`. Using the wrong format specifier invokes undefined behavior. For example on 64-bit systems `size_t` is probably 64 bits and you might not get the correct answer – phuclv Nov 16 '16 at 12:47
  • @RR84 to print sizes you must use [`%zu`](http://stackoverflow.com/q/940087/995714) – phuclv Nov 16 '16 at 12:48
  • 1
    @RR84 *This is a piece of example code from a school lecture, I didn't write it.* Why would one need or even want a *signed* `long` to count the number bits set to `1` in an `unsigned long`, but count at most a number equal to the number of bytes in `int`? And then, why pass an `unsigned long` in the first place? And, [the proper `printf` format specifier](http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html) for the `size_t` returned by `sizeof()` is `%zd`, although `%zu` would be much better, as `size_t` is `unsigned`. A bare `%d` for a `size_t` is undefined behavior – Andrew Henle Nov 16 '16 at 12:48
  • @LưuVĩnhPhúc Yes, but %d will work on more compilers, and is sufficient in this case. – torhu Nov 17 '16 at 10:31
  • @torhu who told you that? `%d` will definitely won't work on most (if not all) 64-bit compilers. [Using the wrong format specifier invokes undefined behavior](http://stackoverflow.com/q/16864552/995714) and anything can happen. `%zu` works in any modern standard-compliant compiler – phuclv Nov 17 '16 at 10:39
  • It works in practice. But it wouldn't work on a big endian system, for instance. See also http://stackoverflow.com/questions/15610053/correct-printf-format-specifier-for-size-t-zu-or-iu – torhu Nov 17 '16 at 12:06
  • @torhu not it wouldn't work reliably in practice, regardless of endianness, because it's **undefined behavior**. [What Every C Programmer Should Know About Undefined Behavior](http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html). About the other link: was MSVC standard-compliant? – phuclv Nov 17 '16 at 13:55
  • Unfortunately, Microsoft doesn't seem to care much about C99. And can you name one (relatively popular) compiler for x86_64 where it wouldn't work in practice? – torhu Nov 19 '16 at 20:42

1 Answers1

0

The size of "int" depends on your machine's architecture. If you're compiling on a 32bit, it will probably be 4bytes(thus 8*4 = 32, that you expected)

On AMD64, you'll have to use int32_t to make it be 4 bytes.

Julio P.C.
  • 321
  • 1
  • 7
  • 1
    "On AMD64, you'll have to use int32_t to make it be 4 bytes" nope, it depends on the data model. On my x86-64 linux system, `sizeof(int)` is 4. See https://en.wikipedia.org/wiki/64-bit_computing – davmac Nov 16 '16 at 12:23
  • there are rarely any architectures with 64-bit int nowadays – phuclv Nov 16 '16 at 12:49