Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
1 integer 4 bytes

Subject: 1 integer 4 bytes

From: Jose

Date: 22 Jul, 2010 17:22:22

Message: 1 of 7

Hello to everyone, why when i have to send 1 integer, i.e the number 5, i have to send 4 bytes...i.e...why 1 integer correspond with 4 bytes, and not 1 byte, thank you
in advance?

Subject: 1 integer 4 bytes

From: us

Date: 22 Jul, 2010 17:31:40

Message: 2 of 7

"Jose " <jose.l.vega@gmail.com> wrote in message <i29uoe$5la$1@fred.mathworks.com>...
> Hello to everyone, why when i have to send 1 integer, i.e the number 5, i have to send 4 bytes...i.e...why 1 integer correspond with 4 bytes, and not 1 byte, thank you
> in advance?

a hint:

     help int8; % <- or: UINT8
% eg,
     v=int8(5);
     whos v;
%{
  Name Size Bytes Class Attributes
  v 1x1 1 int8
%}

us

Subject: 1 integer 4 bytes

From: Sean

Date: 22 Jul, 2010 17:33:07

Message: 3 of 7

"Jose " <jose.l.vega@gmail.com> wrote in message <i29uoe$5la$1@fred.mathworks.com>...
> Hello to everyone, why when i have to send 1 integer, i.e the number 5, i have to send 4 bytes...i.e...why 1 integer correspond with 4 bytes, and not 1 byte, thank you
> in advance?

>>class(your_integer)
it's either a 32bit integer or a single. If it's between [0, 255] recast it as an unsigned 8bit integer:
uint8(your_integer);

Subject: 1 integer 4 bytes

From: Walter Roberson

Date: 22 Jul, 2010 17:55:51

Message: 4 of 7

Jose wrote:
> Hello to everyone, why when i have to send 1 integer, i.e the number 5,
> i have to send 4 bytes...i.e...why 1 integer correspond with 4 bytes,
> and not 1 byte, thank you
> in advance?

Please expand on what you mean by "send" for the purposes of your question?

Are each of the values you need to "send" small enough to fit within single
bytes? If not, then is the pattern of which values will be larger known in
advance?

If the values you need to "send" may vary in size then there are techniques
that can be used to vary the amount of data that needs to be sent, but these
techniques require specific code to read the values afterwards and are not
compatible with standard data storage or transmission interfaces; whether the
techniques will reduce the total amount of data to be "sent" will depend upon
the statistical average length of the data. For more information about these
kinds of techniques, I recommend the book "Text Compression" by Bell, Cleary,
and Witten. In particular, Cleary and Witten have written a number of papers
on data compression techniques.

Subject: 1 integer 4 bytes

From: Jose

Date: 22 Jul, 2010 18:13:04

Message: 5 of 7

Walter Roberson <roberson@hushmail.com> wrote in message <i2a0ua$8m3$1@canopus.cc.umanitoba.ca>...
> Jose wrote:
> > Hello to everyone, why when i have to send 1 integer, i.e the number 5,
> > i have to send 4 bytes...i.e...why 1 integer correspond with 4 bytes,
> > and not 1 byte, thank you
> > in advance?
>
> Please expand on what you mean by "send" for the purposes of your question?
>
> Are each of the values you need to "send" small enough to fit within single
> bytes? If not, then is the pattern of which values will be larger known in
> advance?
>
> If the values you need to "send" may vary in size then there are techniques
> that can be used to vary the amount of data that needs to be sent, but these
> techniques require specific code to read the values afterwards and are not
> compatible with standard data storage or transmission interfaces; whether the
> techniques will reduce the total amount of data to be "sent" will depend upon
> the statistical average length of the data. For more information about these
> kinds of techniques, I recommend the book "Text Compression" by Bell, Cleary,
> and Witten. In particular, Cleary and Witten have written a number of papers
> on data compression techniques.


ok, guys, I understood...for default, when i am writing in my matlab
v=5...is the same as v=uint64(5)...it means 1 integer correspond to 8 bytes (64 bits).

Cheers guys.

Subject: 1 integer 4 bytes

From: us

Date: 22 Jul, 2010 18:19:06

Message: 6 of 7

"Jose "
> ok, guys, I understood...for default, when i am writing in my matlab
> v=5...is the same as v=uint64(5)...it means 1 integer correspond to 8 bytes (64 bits).
>
> Cheers guys.

no...

     v=5; % <- the equivalent of...
     w=double(5); % <- ...the def ML data type...

us

Subject: 1 integer 4 bytes

From: Walter Roberson

Date: 22 Jul, 2010 18:59:41

Message: 7 of 7

Jose wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <i2a0ua$8m3$1@canopus.cc.umanitoba.ca>...
>> Jose wrote:
>> > Hello to everyone, why when i have to send 1 integer, i.e the number
>> 5, > i have to send 4 bytes...i.e...why 1 integer correspond with 4
>> bytes, > and not 1 byte

> ok, guys, I understood...for default, when i am writing in my matlab
> v=5...is the same as v=uint64(5)...it means 1 integer correspond to 8
> bytes (64 bits).

If Matlab automatically used the least storage needed for a value, what would
you expect it to do in the case

v = 5;
v(2) = 99999;

If you expect that this kind of minimal storage should only happen for scalar
variables and that all non-scalar arrays should automatically be extended to
the more general size, then what would you expect it to do in the case of

v = int8(5);
v(2) = 99999;

Would you expect it to keep an internal flag for scalars about whether the
type had been set explicitly or according the the range?

Would you expect Matlab to continually adjust the storage size for a variable
that is being changed in a loop?

for K = 1:10000
   v = mod(K^43 + 17*K + 5, 2^32);
end

some of those v values will be in the range 0 to 255 and some of them will be
256 to 65535 and some of them will be larger: should Matlab change the
variable size each time? If you expect it to do that, then what is your
expectation on how Matlab should do so and yet produce machine efficient code?
CPUs take different amounts of time to work with different sizes; it is not
uncommon for either 32 bit or 64 bit integers to be the fastest integers and
it is common on modern computers for 8 bit integers to be the slowest of the
integer accesses.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us