Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Confuse with int, int signed and int unsigned

Status
Not open for further replies.

vead

Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Activity points
3,815
Hello
I am confuse with int, int signed and int unsigned. The int type stores integers. it can store positive , negative number

For example If the size of working register is 8 bits (1 Byte). The maximum number 8 bits can hold (11111111 in binary) is 255 in the decimal system. So, the numbers from 0 to 255 can fit within 8 bits.

unsigned ranges between 0 and +255
signed ranges from -128 to +127
int range between 0 and +255 or -128 to +127


1) unsigned ranges between 0 and +255

it can be store number like 0,1, 10, 50, 100, 255
it can't store number greater then 256

2) signed ranges from -128 to +127

it can store number like -128, -105, 1, 96....
it can not store number like -129, -136

3) int range between 0 and +255 or -128 to +127

it can be store number like 0,1, 10, 50, 100, 255
it can't store number greater then 256,

it can store number like -128, -105, 1, 96....
it can not store number like -129, -136
 

Re: confuse with int, int signed and int unsigned

Hello
I am confuse with int, int signed and int unsigned. The int type stores integers. it can store positive , negative number

For example If the size of working register is 8 bits (1 Byte). The maximum number 8 bits can hold (11111111 in binary) is 255 in the decimal system. So, the numbers from 0 to 255 can fit within 8 bits.

unsigned ranges between 0 and +255
signed ranges from -128 to +127
int range between 0 and +255 or -128 to +127


1) unsigned ranges between 0 and +255

it can be store number like 0,1, 10, 50, 100, 255
it can't store number greater then 256

2) signed ranges from -128 to +127

it can store number like -128, -105, 1, 96....
it can not store number like -129, -136

3) int range between 0 and +255 or -128 to +127

it can be store number like 0,1, 10, 50, 100, 255
it can't store number greater then 256,

it can store number like -128, -105, 1, 96....
it can not store number like -129, -136

Is there a question in there somewhere?
 

Re: confuse with int, int signed and int unsigned

Is there a question in there somewhere?
I have explained question with answer. I just want to be sure weather my understanding is right or wrong with example ?
 

Re: confuse with int, int signed and int unsigned

Hi,

3) int range between 0 and +255 or -128 to +127
I'm no software specialist..but in my work I'm involved with a lot of source codes.

Every time I see definitions like "int" I have to ask if it is signed or not and if it is 8 bit or 16bit.
Therefore I like definitions like "int8, uint8, int16, uint16".

I'm not sure if every compiler treats "int" the same way, but I'm convinced that one compiler can not use an "int" variable with a range of 0..255 and -128...127 at the same time. Either it treats it as signed or as unsigned. Maybe different compilers treat them differently.

Again: I no specialist.
Maybe someone with more experience can clarify this.

Klaus
 

Re: confuse with int, int signed and int unsigned

I just want to be sure weather my understanding is right or wrong with example ?

Your conception is right except the following.
1. Size of integer is not one byte.Depending upon system, it is 2 or 4 byte in size.
2. "Int" is actually "signed int". So your explanation under Sl. No.3 is not right. It should be -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647.

- - - Updated - - -
 

Re: confuse with int, int signed and int unsigned

Depends of compilier. For STM IAR int takes 2 bytes and can be -32768 to +32768. For ARM Keil it takes 32 bit == 4 bytes.
This can be easily checked by using 'sizeof' macros: sizeof(int) will return 4 in keil and 2 in iar.
Plain 'char' type can be signed or unsigned depends of settings in keil, by default it is unsigned. This doesn't really matter, char normally used for storing string characters.
For math operation I'm also prefer to use more visible types like uint8_t or int32_t.
Good question about enumerated typed. Enums by definiton of C should be uint. Normally we also don't care much about that...
 

Re: confuse with int, int signed and int unsigned

Depends of compilier. For STM IAR int takes 2 bytes and can be -32768 to +32768. For ARM Keil it takes 32 bit == 4 bytes.
This can be easily checked by using 'sizeof' macros: sizeof(int) will return 4 in keil and 2 in iar.
Plain 'char' type can be signed or unsigned depends of settings in keil, by default it is unsigned. This doesn't really matter, char normally used for storing string characters.
For math operation I'm also prefer to use more visible types like uint8_t or int32_t.
Good question about enumerated typed. Enums by definiton of C should be uint. Normally we also don't care much about that...

Thanks. Have you noticed one thing in my post #1. I said " For Example " I was just assuming register is 8 bit wide

What is difference between char, unsigned char and signed char ?

char data type is used to represent character

assume register is 8 bit wide

unsigned char ranges between 0 and +255
signed char ranges from -128 to +127
char range between 0 and +255 or -128 to +12

char letter = 'a'; * in ASCII value, 'a' = 97 */


what is meaning of signed char ? how does it store information of character because it has positive and negative range? can someone explain with example
 

Re: confuse with int, int signed and int unsigned

No matter how many bits an 'int' or 'char' occupies, the rule is that the most significant BIT is reserved for the polarity.
If it is UNsigned, the bit is allocated to the number range so it essentially becomes the MSB of the number.
If it is signed (normally the default) the bit is zero for a positive number and one for a negative number.

Obviously when the bit is used as part of the value (unsigned), being binary it doubles the available number range but there is no way of representing whether it is positive or negative.

Brian.
 

Re: confuse with int, int signed and int unsigned

For a detailed explanation, you should read a C text book, e.g. good old Kernighan Ritchie, The C programming language.

The char integer type can be used for other purposes than representing ASCII codes, respectively signed char can be useful. You can use it for any numeric operation with restricted range. It makes particularly sense for 8-Bit machines like PIC16/18, or generally for the compact storage of small numbers.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top