difference between int and long

I

Imran

hello all

I am bit confused with int and long data type.

I read in some book that, int cal hold 2^16 values
where in long can hold 2^32. and both data types are 4 bytes long.

My doubt this, since both are 4 bytes , how can it hold different size?

Thank you
 
O

Ondra Holub

Imran napsal:
hello all

I am bit confused with int and long data type.

I read in some book that, int cal hold 2^16 values
where in long can hold 2^32. and both data types are 4 bytes long.

My doubt this, since both are 4 bytes , how can it hold different size?

Thank you

Standard guarantees only that int is at least 16-bit and long is at
least 32-bit and sizeof(int) <= sizeof(long).

So for example on typical 32-bit platform are int and long both 32-bit,
on 64-bit Linux can be int 32-bit and long 64-bit etc.
 
S

Scott McPhillips [MVP]

Imran said:
hello all

I am bit confused with int and long data type.

I read in some book that, int cal hold 2^16 values
where in long can hold 2^32. and both data types are 4 bytes long.

My doubt this, since both are 4 bytes , how can it hold different size?

Thank you

If they are both 4 bytes then they can both hold 2^32. The difference
between int and long depends on the type of processor. On many
processors today they are exactly the same.
 
P

Philipp Reh

If they are both 4 bytes then they can both hold 2^32. The difference
between int and long depends on the type of processor. On many
processors today they are exactly the same.

That is not true. C++ defines a byte in terms of the size of a character
which must be at least 8 bits.
The meaning of "4 bytes" thus is 4 times the size of char which doesn't
have to equal 32 bits in size.
 
G

Grizlyk

Philipp Reh write:
That is not true. C++ defines a byte in terms of the size of a character
which must be at least 8 bits.
The meaning of "4 bytes" thus is 4 times the size of char which doesn't
have to equal 32 bits in size.

It seems to me, that there is no keyword "byte" in C++ programs.
Outside of C++, most people treat bit as bit and byte as 8 bit - fixed
size of "byte" is more useful, because a man can understand concrete
size of memory in bits. I think "byte" is language independent system
independent memory size, similar to "bit".

But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,
a) char by char describe continuous memory ( memory without holes and
lost bits )
b) char is suitable for all other types
1) as align bound of the type
2) number of chars per the type is integral number (not float)
c) sizeof() of char is always "1"
d) each type has MAX_SIZE according only to memory size - if "char" is
16 bit, char can hold values 0x0000 - 0xffff, not 0x00 - 0xff.
e) 8bit<=char<=short<=int<=long

On x86 "char" number of "bits" is equal to "byte" number of "bits".

The reason to declare type with sizeof==2, aligned in memory to
sizeof==4 boundary is allow packed storage in external memory or packed
structures, aligned to sizeof==2.
 
A

Alf P. Steinbach

* Grizlyk:
Philipp Reh write:


It seems to me, that there is no keyword "byte" in C++ programs.

That's right, but in the C++ standard and C++ context (or C, for that
matter) 'byte' means 'char' and vice versa.

Outside of C++, most people treat bit as bit and byte as 8 bit - fixed
size of "byte" is more useful, because a man can understand concrete
size of memory in bits.

Yep, that's the old "eat dung" argument, that twenty zillion flies can't
be wrong. Yet, while they're undoubtedly doing what's right for them,
and so aren't wrong about eating that dung, the inference that the same
behavior is appropriate for, say, humans, is incorrect. There's more
than one meaning of 'byte' -- after all, language evolves -- and in a
technical context where the number of bits matters the 8 bit entity is
more properly and precisely called an 'octet' (mostly this is used by
electrical engineers and the telecommunications/network industry).

I think "byte" is language independent system
independent memory size, similar to "bit".

Not in the context of C++. Look it up at NIST, IEC or really anywhere.
Outside the context of C++, it is a /system dependent/ size. Which is
why 'octet' is used when it's necessary to be precise.

But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,

Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
char is 1).
 
M

Mike Wahler

Grizlyk said:
Philipp Reh write:


It seems to me, that there is no keyword "byte" in C++ programs.

No, the keyword is 'char'. The standard (IEC 14882) defines 'char'
as being a byte.
Outside of C++, most people treat bit as bit

A bit is a bit.
and byte as 8 bit - fixed

That's not always true. The proper term for an eight-bit object
is 'octet'.
size of "byte" is more useful, because a man can understand concrete
size of memory in bits.

Except that each platform defines what 'concrete' is. :)
I think "byte" is language independent system

It's platform dependent. Some use different size bytes than others.
that's why we have the 'CHAR_BIT' macro (and
std::numeric_limits said:
independent memory size, similar to "bit".

But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,

Both C and C++ define 'char' as meaning *exactly* 'byte', whatever
the size of a byte on the host platform happens to be. However, both
languages require that the size of a char (i.e. byte) be at least eight
bits.

So on any system, regardless of how many bits in a byte, sizeof(char) == 1.
Always.

-Mike
 
G

Grizlyk

Alf said:
Yep, that's the old "eat dung" argument, that twenty zillion flies can't

No, it is not "eat dung". Ii is standard international (SI) measure of
information or storage capacity, used by most people in practical
cases, similarly to meter or second.
Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
char is 1).

You are right formally, but fortunatelly, the part of standard can be
silently ignored at practical cases, as well as we can silently ignore
existence of keyword "goto". I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.

And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
- C++ specific memory size as base for all types.
 
A

Alf P. Steinbach

* Grizlyk:
No, it is not "eat dung". Ii is standard international (SI) measure of
information or storage capacity, used by most people in practical
cases, similarly to meter or second.


You are right formally, but fortunatelly, the part of standard can be
silently ignored at practical cases, as well as we can silently ignore
existence of keyword "goto". I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.

And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
- C++ specific memory size as base for all types.

Manual Plonk.
 
K

Kai-Uwe Bux

Grizlyk said:
No, it is not "eat dung". Ii is standard international (SI) measure of
information or storage capacity, used by most people in practical
cases, similarly to meter or second.


You are right formally, but fortunatelly, the part of standard can be
silently ignored at practical cases, as well as we can silently ignore
existence of keyword "goto".

Actually, we cannot ignore either of those in this group: neither for
practical purposes nor (and more importantly) for discussing C++.
I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.

Sure. It is somewhat unfortunate that the C++ standard is intruding quite a
bit into the ordinary English language by specializing the meanings of
common terms such
as "exception", "object", "undefined" / "unspecified" / "implementation
defined" (behavior) or, in this case, "byte". However, communication in
this group is greatly simplified if everybody just sticks to two simple
conventions:

a) If the standard defines the meaning of a term, do not use that term with
a different meaning without explicitly indicating so.

b) If the standard provides a term for a concept and you mean that concept,
use the terminology used in the standard.

As these two rules of thumb have proven quite useful, regulars tend to be
quite opinionate about breaking those rules willfully.

Note that I am not arguing that you should not use "byte" in its ordinary
meaning outside this group. But within this group, it has proven useful to
avoid confusion arising from competing meanings. The resolution of such
competition is: the meaning used in the standard wins.

And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
- C++ specific memory size as base for all types.

That "byte" is not a keyword does not make it any less C++ specific
terminology as, say, "object", which is also not a keyword.


Best

Kai-Uwe Bux
 
G

Grizlyk

Kai-Uwe Bux said:
Sure. It is somewhat unfortunate that the C++ standard is intruding quite a
bit into the ordinary English language by specializing the meanings of
common terms such
as "exception", "object", "undefined" / "unspecified" / "implementation
defined" (behavior) or, in this case, "byte". However, communication in
this group is greatly simplified if everybody just sticks to two simple
conventions:

Are you really do not see defferences between SI definitions and other
"words" in the world?

Well, maybe "byte" is not defined in SI, but the fixed size of byte (8
bit) is conventional for most peolpe in the world. And no one "cometee"
of any language can not force people to have more than one "byte" with
different meaning, as no one "cometee" can not introduce two types of
"dollar": "ordinary dollar"- 100-cents, and "C++ dollar" - with
variable number of cents, for example 315 cents.

Especially, when C++ has own replacement for "C++ specific byte" - it
is "char". There is no sence to have in C++ "byte" definition (which
constradiscts to ordinary "byte" definition), because "char" exist.

To discuss "to define 'C++ specific byte' or do not" is time wasted.
 
K

Kai-Uwe Bux

Grizlyk said:
Are you really do not see defferences between SI definitions and other
"words" in the world?

Well, maybe "byte" is not defined in SI, but the fixed size of byte (8
bit) is conventional for most peolpe in the world.

True and already conceded.
And no one "cometee"
of any language can not force people to have more than one "byte" with
different meaning, as no one "cometee" can not introduce two types of
"dollar": "ordinary dollar"- 100-cents, and "C++ dollar" - with
variable number of cents, for example 315 cents.

Actually, it can. In the very same way that mathematicians have overloaded
the terms "group", "ring", "field", "loop", etc. Once introduced by the
standard, the specialized meanings are default meanings In This News Group.
Especially, when C++ has own replacement for "C++ specific byte" - it
is "char". There is no sence to have in C++ "byte" definition (which
constradiscts to ordinary "byte" definition), because "char" exist.

To discuss "to define 'C++ specific byte' or do not" is time wasted.

Whatever.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top