0

I have seen the link What does it mean by word size in computer? . It defines what word size is. I am trying to represent very long string in bits where each character is represented by 4 bits and save it in long or integer array so that I can extract my string when required. I can save the bits either in integer array or long array.

  1. If I use long array (8 bytes) I will be able to save 8*4=32 bits in one long array.
  2. But if I use int I will be able to save 4*4=16 bits only.

Now, if I am given my Word Size=32 then is it the case that I should use int only and not long.

Community
  • 1
  • 1
likeGreen
  • 1,001
  • 1
  • 20
  • 40
  • 7
    [What does the C++ standard state the size of int, long type to be?](https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be) – Cory Kramer Aug 20 '15 at 20:24
  • 4 bytes and 8 bytes respectively – likeGreen Aug 20 '15 at 20:25
  • 3
    Go read the above link, you are wrong, that is why I linked it. – Cory Kramer Aug 20 '15 at 20:27
  • Use the longest type you can - which is `long long`, not either of your options. The compiler and standard library are perfectly capable of handling this regardless of the local word size. – Alan Stokes Aug 20 '15 at 20:34
  • @CoryKramer I checked the sizeof(long) and sizeof(int) on my system which was 8 and 4 based on which I was working. But then this link clarifies something for me http://stackoverflow.com/questions/18901080/why-is-the-sizeofint-sizeoflong – likeGreen Aug 20 '15 at 20:48
  • 1
    *"I checked the sizeof(long) and sizeof(int) **on my system**..."* that is the important part – Cory Kramer Aug 20 '15 at 20:49

1 Answers1

14

To answer your direct question: There is no guaranteed relationship between the natural word-size of the processor and the C and C++ types int or long. Yes, quite often int will be the same as the size of a register in the processor, but most 64-bit processors do not follow this rule, as it makes data unnecessarily large. On the other hand, an 8-bit processor would have a register size of 8 bits, but int according to the C and C++ standards needs to be at least 16 bits in size, so the compiler would have to use more than one register to represent one integer [in some fashion].

In general, if you want to KNOW how many bits or bytes some type is, it's best to NOT rely on int, long, size_t or void *, since they are all likely to be different for different processor architectures or even different compilers on the same architecture. An int or long may be the same size or different sizes. Only rule that the standard says is that long is at least 32 bits.

So, to have control of the number of bits, use #include <cstdint> (or in C, stdint.h), and use the types for example uint16_t or uint32_t - then you KNOW that it will hold a given number of bits.

On a processor that has 36-bit "wordsize", the type uint32_t for example, will not exist, since there is no type that holds exactly 32-bits [most likely]. Alternatively, the compiler may add extra instructions to "behave as if it's a 32-bit type" (in other words, sign extending if necessary, and masking off the top bits as needed)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • humm is that true? i thought that on on a 36 bit "wordsize" they will just emulate a uint32_t with a 36 bit item and C/C++ will hide the extra space from you? I will see if I can find anything in standard about this though, I'm not sure – Chris Beck Aug 20 '15 at 20:45
  • @Mats Petersson Isn't it fine that I check the sizeOf(long) and sizeOf(int) on my system and work based on its result – likeGreen Aug 20 '15 at 20:52
  • sizeof(sometype) * CHAR_BIT (from ``) will tell you how many bits. This is because `sizeof()` will give you the size in bytes, which is the size of `char`. So in the example of a 36-bit machine, a `char` would have `sizeof(char)==1`, but `CHAR_BIT == 9`, rather than the more common `CHAR_BIT==8` - of course, 36-bit machines are rather uncommon. But they do exist. – Mats Petersson Aug 20 '15 at 21:06
  • Sure you want to name the C++-only adaption of the standard C header, instead of the latter? – Deduplicator Aug 20 '15 at 21:33
  • 1
    @ChrisBeck, no `uint32_t` and similar are, if they exist, guaranteed to have exactly the width, no padding. There might also be a type `uint_least32_t` that behaves similar to what you describe. – Jens Gustedt Aug 20 '15 at 21:39
  • The first paragraph relieves my worry that I got a 32-bit CPU after seeing that `sizeof(int)` is 4 on my machine. – aafulei Dec 23 '19 at 10:24