Bytes And Bits

Pages: 12
Is there a place/book to learn bites and bites such as types and how much bytes/bits they take for each diffrent type and what are they

and do i have to know these ranges to be a good C++ programmer
They can vary on different compilers

bool are generally 1 byte
char are generally 1 byte
short are generally 2 bytes and not smaller than chars
int are generally 4 bytes and not smaller than shorts
long are generally 4 bytes and not smaller than ints
long long are generally 8 bytes and not smaller than long


1 byte = 8 bits.

You can use sizeof to find the number of bytes a variable is.

http://www.cplusplus.com/doc/tutorial/variables/
Last edited on
char are generally 1 byte

A char is always one byte, at least as the standard defines a byte.


1 byte = 8 bits.

Not necessarily, although with the restrictions imposed by the standard (and if memory serves,) I believe a byte in C++ must always have at least 8 bits.

To find out the sizes for your environment, see: http://en.cppreference.com/w/cpp/types/numeric_limits

http://en.cppreference.com/w/cpp/types/numeric_limits/digits in particular.
Thanks guys but what kind of stuff do not operating systems programmers do with bytes or bits or embedded systems programmers
The standard or a byte is 8 bits, but it can vary depending on the compiler being used. A bit is the smallest unit of data measurement: it is either 1, or 0. A bit is represented by single-digit binary number (base-2 integer, aka 0, 1, 10, 11, 100, 101, 110, 111, ...). If you do some research, all you have to do is apply what you know about counting in base 10 (0-9, aka 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...) to count in base-2 and base-16. :) This can also be used for hashing if you want to go further, when you use this to conver one base to another.
Last edited on
The number of bits in a byte can vary? I had no idea. Just checked wikipedia (very reliable) and it confirmed that. But does anyone have a single example of a target machine/processor with a byte-size which is not 8 bits?

I've seen processors which take 24 bit numbers instead of 32. Therefore integers compiled for these processors are not the standard 32 bits. But that's really the only variation of the standards that I've ever heard of.
Well, it's not common that I know of. Maybe more so on embedded and more so in the past. Parashift has some examples: http://www.parashift.com/c++-faq-lite/very-large-bytes.html
That link describes that C++ allows this. I have no doubt of that. However wouldn't that depend more on the processor and OS architecture than the language used?

To communicate on a network with anything with TCP/IP you'd need to use 8-bit bytes. Any x84 x64 or ARM processors I believe would need 8-bit bytes. OSs might need to swap endian, but the size of the bytes are constant. Or perhaps I'm confused regarding the definition of a byte.

Edit: How about the definition of a nibble? Does it have any place in C++ or computer science in general? I generally think of a nibble as a single hex digit. A byte like 0x5c contains 2 nibbles: 0x5 and 0xC.
Last edited on
Stewbond wrote:
But does anyone have a single example of a target machine/processor with a byte-size which is not 8 bits


A quick web browse picks up

CHAR_BIT=9
PDP-6, PDP-10 (obsolete) , but you can check out the gcc port at http://pdp10.nocrew.org/gcc/download/gcc-pdp10-20020611.tar.bz2
Unisys ClearPath Dorado servers supposely use this for backwards compatibility with UNIVAC

CHAR_BIT=16, uint8_t is not defined:
Texas Instruments C54x, C62xx and C64xx DSPs
Texas Instruments TMS32F28xx
BlueCore-5 chip from Cambridge Silicon Radio

CHAR_BIT=24
BelaSigna DSPs from On Semi
DSP56K/Symphony Audio DSPs from Freescale
DSP 56300 family

CHAR_BIT=32
Analog Devices 32-bit SHARC DSP
DSP 56156 from Motorola

I am sure there's more
Last edited on
Univac uses 9 bit bytes? Very cool. I've never programmed for one, or even used the terminal, but I've installed machines in rooms where these were sitting. I love how instead of having monitors they had printers with massive scrolls of paper. You had to read the most recent printouts to see what was sent out by the application. Life is so much simpler these days...
More comments guys lets make this post popular

What kind of stuff do i need to know to make a emulator
Just my 8 cents worth :)

With TCP/IP there is a term octet which means 8 bits, so this avoids confusion about byte size. My basic understanding of it says that it doesn't necessarily mean that the information being sent is 8 bits per byte, as long as the communication signals have the correct number of bits, then the sending and receiving of information is interpreted by the OS. If TCP/IP defines everything in terms of octets then it will be easier for a TCP/IP application if a byte is 8 bits.

I have heard of Mainframes which run on 7 bits per byte, also of certain embedded systems on GPS satellites with only 5 bits per byte. And that 8 bits per byte was a thing that started with MS-DOS. Or at least it is a standard on PC's but not necessarily on other machines.

Guys where can i learn all this low level undertstanding

and what is TCP/IP
Guys where can i learn all this low level undertstanding


Any decent C++ book has a section on bitwise operations and usually they will go into a good amount of detail on what bytes and bits are. There is also plenty good resources on the internet like http://www.cprogramming.com/tutorial/bitwise_operators.html that can be helpful.

Just start digging and googling around I would say otherwise pick up a good C++ book like C++ Primer and dig into their section on it.

and what is TCP/IP


TCP/IP is a networking protocol that a lot of applications make use of. Well to be more exact it is actually two protocols TCP and IP. It would be a very lengthy forum post to get into all the details of TCP/IP, but a quick Google search for the basics on networking will provide you plenty of information.

But to sum it up TCP is a reliable stream based protocol. It guarantees delivery of the data that is transmitted unlike unreliable protocols like UDP which if a packet is lost it just continues on.

Another good place to look is TCP's RFC's which can be found here http://www.ietf.org/rfc/rfc793.txt
Last edited on
Thanks I was soo close to using c++ primer but when I go on the sale item class it dosent work goes to the different

And could you recomend me any other bookks
closed account (z05DSL3A)
Mizfizz wrote:
Guys where can i learn all this low level undertstanding
Study, practice, and time. There is no silver bullet, just the wide expanse of computer science[1].

and wrote:
And could you recomend me any other bookks
You ask this a lot, have you not got enough recommendations yet? At the moment, form what seen from you, you seem to be flitting around without having the basics do yet. You need to make a decision on what book (or other resource) you are going to start with and start at the beginning. When you have a problem, come here and ask specific questions about the problem you are having.

--------------------------------------------------------------------------------
[1] http://en.wikipedia.org/wiki/Computer_science
Thanks
Guys any other information you guys can give
closed account (z0My6Up4)
What do you mean by this?

Thanks I was soo close to using c++ primer but when I go on the sale item class it dosent work goes to the different


What do you think is wrong with C++ Primer?
Not the book but the sales_itemclass header didn't work cue i couldn't download it
closed account (z0My6Up4)
Oh what was the problem you were having? The source code is still available for download on their website. Here is the link: http://www.informit.com/store/c-plus-plus-primer-9780321714114

I think the sales_item.h file is in chapter 1 when you unzip it.
Pages: 12