View Full Version : Signed Ints??
mightywombat
22-02-2004, 03:16
Has anyone had any experience using negative numbers in their code? I remember trying to do something with negative numbers on the eduBOT and I think I ran into trouble. I was thinking that maybe if you declared it
signed int variable;
then maybe it would work.... I'm not sure though. Any suggestions?
Thanks.
Bill
seanwitte
22-02-2004, 10:05
Yes, you can used signed ints in your code. We're using a header file with the following types defined in it:
//data type definitions for integers - C18
//uchar
//size: 8 bits (1 byte)
//range: 0 to 255
typedef unsigned char uchar;
//schar
//size: 8 bits (1 byte)
//range: -128 to 127
typedef signed char schar;
//uint16
//size: 16 bits (2 bytes)
//range: 0 to 65535
typedef unsigned int uint16;
//sint16
//size: 16 bits (2 bytes)
//range: -32768 to 32767
typedef signed int sint16;
//uint24
//size: 24 bits (3 bytes)
//range: 0 to 16,777,215
typedef unsigned short long uint24;
//sint24
//size: 24 bits (3 bytes)
//range: -8,388,608 to 8,388,607
typedef signed short long sint24;
//uint32
//size: 32 bits (4 bytes)
//range: 0 to 4,294,967,295
typedef unsigned long uint32;
//sint32
//size: 32 bits (4 bytes)
//range: -2,147,483,648 to 2,147,483,647
typedef signed long sint32;
So, if you want a 4-byte signed integer you can declare it as a sint32. An unsigned 3-byte integer is a uint24, etc.
Kinda on a similair topic, how can you create a single-bit variable - kinda like a bool - using c?
This (http://www.chiefdelphi.com/forums/papers.php?s=&action=single&paperid=272) whitepaper says you can use individual bits like so:
struct bits {
unsigned int itsy: 3; // Use 3 bits as the variable ?itsy?
unsigned int bitsy: 3; // Use 3 bits as the variable ?bitsy?
unsigned int teensy: 1; // Use 1 bit as the variable ?teensy?
unsigned int weensey:1 // Use 1 bit at the variable ?weensy?
};
but the "itsey: 3;" syntax doesn't seem to work unless you are using it inside a struct. I'm sure there's a way more elegant than using a struct, but can anyone let me know what it is?
Kinda on a similair topic, how can you create a single-bit variable - kinda like a bool - using c?
This (http://www.chiefdelphi.com/forums/papers.php?s=&action=single&paperid=272) whitepaper says you can use individual bits like so:
struct bits {
unsigned int itsy: 3; // Use 3 bits as the variable ?itsy?
unsigned int bitsy: 3; // Use 3 bits as the variable ?bitsy?
unsigned int teensy: 1; // Use 1 bit as the variable ?teensy?
unsigned int weensey:1 // Use 1 bit at the variable ?weensy?
};
but the "itsey: 3;" syntax doesn't seem to work unless you are using it inside a struct. I'm sure there's a way more elegant than using a struct, but can anyone let me know what it is?
Well, that's C for you. If you want to work with bits, you're stuck with bitfields as the most elegant solution. Of course, you could use ints and just use macros to get at the bits -- but that is messy, and why not let the compiler do it for you automagically with the bitfield? There's a struct already declared for you by IFI in the default code that gives you access to bits 1 - 8. If you desire true "elegance" might I suggest just hiding everything behind a define statement -- e.g., #define mybit bitfield.bit5.
deltacoder1020
22-02-2004, 12:42
the problem with having a single bit variable is that memory allocation works in bytes - you create a variable, it's gonna take up at least a byte no matter how you look at it. thus, you either have to tell the compiler explicitly to put 8 1-bit variables together (i.e. using struct to create a bitfield), or just go with byte-long variables and just give them a 0 or 1 value, using an unsigned char.
I'm not sure if you had it without the signed part of it, but I think this compiler is a little weird in that it defaults to unsigned. So, you have to include the signed if you want negative. Of course, like every single one of my posts, I could be wrong.
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.