Hi there,

I need explanation for the range of data type say character which is 1byte ranges from -127 to 127 or 0 to 255. I did not understand what it means and how can I check the range of characters.

Does it means it can store only values from -127 to 127 ?

Anyone please help me to understand this concept and also tell me ranges for singed and unsinged data types. Thanks !

Regards

Recommended Answers

All 4 Replies

How many different codes can 8 bits hold?
...........................16 .........?
...........................32 .........?
...

What you want each unique bit pattern to mean, is up to you.

Do you know what 2's compilement means? (If not, Google it.)

So, 8 bits has 256 unique code that could represent the numbers 0..255
or
-128..127

or any other 256 items you might chosse to encode and then to decode.

Range means the maximum and minimum value that can be stored inside the variable of a given type. For example if you have unsigned char and if we assume that the size of the datatype is 8 bits then you can store values ranging from 0 - 2^8-1 i.e. 0-255 inside it.

An 8 bit value, such as a char, can be signed, or unsigned. So let's look at chars - you can have a signed or unsigned char. The top-most bit for a signed char is the sign. If set, the value is negative. If not, it is positive. In this case, we have only 7 bits for the actual value (plus the sign bit which == -1).
00000001 == 1
00000010 == 2
00000100 == 4
00001000 == 8
00010000 == 16
00100000 == 32
01000000 == 64

Add them up (1+2+4+8+16+32+64) and you get 127.
if you have 10000000, then you have -1. If all the other bits are set, add them up (negatively) and you get -128.

On the other hand, if the char is unsigned, then 10000000 == 128, so if all bits are set, you get 255. This rule applies to larger integers such as 16, 32, and 64 bit values as well.

Are we sufficiently confused yet? :-)

and if you are still confused after that ^ very detailed explanation, 1 byte = 8 bits, thats how you get 8 numbers 0s or 1s

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.