programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Primitive Numbers Range

Ranch Hand
Posts: 34
I am trying to figure out the ranges of primitive numbers. I came up to the following conclusion, say, for byte range:

BinDec
000000000
01111111127
10000000-128
11111111-1
Howerver I' m not so sure if this is true.
Could anyone help me to clear this out?
Thanks

author
Posts: 9000
19
you got it!!!

Sheriff
Posts: 4313
you got it. Byte's range is -128 to 127 (8 bits)
You can check out the JLS for the other numerical ranges:
�4.2.1 Integral Types and Values

Ranch Hand
Posts: 34
Jessica and Bert, Thank you both for you replies.

Greenhorn
Posts: 4
But how have you reached to this conclusion?

Ranch Hand
Posts: 34
Well, it is said that the leftmost bit is used as the sign of the number. So if it is 0 it is a positive and if it is 1 it is a negative.
For a byte the range is supposed to be for positives: 0 -> 127 and for negatives: -128 -> -1.
The only way I could represent this as bits was what you read above.

mister krabs
Posts: 13974
-2 to the 7th power = -128
(2 to the 7th power) -1 = 127
You can use this formula for all the primitive integers. Replace 7th power with (the number of bits - 1)
It would be easier if HTML was enabled... let's pretend that ** means "to the power of"
byte - 8 bits (-128 to 127) (-2**7 to (2**7)-1)
short - 16 bits (-32,768 to 32,767) (-2**15 to (2**15)-1)
int - 32 bits (-2,147,483,648 to 2,147,483,647) (-2**31 to (2**31)-1)
long - 64 bits (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) (-2**63 to (2**63)-1)

Ranch Hand
Posts: 34