Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Different bit storing

Reply
Thread Tools

Different bit storing

 
 
Chris Mantoulidis
Guest
Posts: n/a
 
      12-18-2003
I have heard the way an int is stored, is different from the way a
short is (for exampe eh... the same could apply for 16bit int and a
long)...

But how are they differently stored?

Is the following correct?

short a=16: 0000000000010000
int b=16 (int int): 00000000000000000000000000010000


in front of the 16 bits of a, are there all 1's?

Plus, I've read about the various ways a signed data type is stored...
What is the mostly used one? If I saw 32bits, how would I know if it
was a signed int or an unsigned int? Would I be able to tell??

Different bit storing seems confusing...
 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      12-18-2003
Chris Mantoulidis wrote:
> I have heard the way an int is stored, is different from the way a
> short is (for exampe eh... the same could apply for 16bit int and a
> long)...
>
> But how are they differently stored?
>
> Is the following correct?
>
> short a=16: 0000000000010000
> int b=16 (int int): 00000000000000000000000000010000
>
>
> in front of the 16 bits of a, are there all 1's?


Usually, there are zero's. Unless, of course, you're on a 16-bit (or
fewer) machine; then the bits before &a might vary from one run of your
program to the next. Or, there may not be anything before &a.

> Plus, I've read about the various ways a signed data type is stored...
> What is the mostly used one?


Two's complement.

> If I saw 32bits, how would I know if it
> was a signed int or an unsigned int?
> Would I be able to tell??


There's no way to know just from the 32 bits.

> Different bit storing seems confusing...


Yep, it's tricky. Usually, you don't have to think about it much.

 
Reply With Quote
 
 
 
 
Kevin Saff
Guest
Posts: n/a
 
      12-18-2003
"Chris Mantoulidis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have heard the way an int is stored, is different from the way a
> short is (for exampe eh... the same could apply for 16bit int and a
> long)...
>
> But how are they differently stored?
>
> Is the following correct?
>
> short a=16: 0000000000010000
> int b=16 (int int): 00000000000000000000000000010000
>
>
> in front of the 16 bits of a, are there all 1's?


Doesn't it depend on the endianness of the system?

On a little-endian machine like mine, the little bytes are stored first, so
for me 16 (020 octal) is stored as:

"\020\000" // short (16 bits for me)
"\020\000\000\000" // int (32 bits for me)

The high 0 bytes are actually stored after the smaller parts of the number.
If I reinterpret_cast an int* to a short*, I get the same value if the int
value fits into the size of a short.

On a big-endian machine the large bytes are stored first, so 16 might be
stored as

"\000\020" // short
"\000\000\000\020" // int

Here reinterpret_cast'ing an int* to a short*, might give 0 instead of 16.

Regardless of which order the bytes are stored, left-shifting (<<) operates
as if the bits were stored high-bit first. So if I keep left shifting
(short (1)) I get something like:

"\001\000"
"\002\000"
"\004\000"
"\010\000"
"\020\000"
"\040\000"
"\100\000"
"\200\000"
"\400\000"
"\000\001"
"\000\002"

etc.

I think you only need to worry when processing binary files from a machine
of opposite endianness.

HTH
--
KCS


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments