Byte-sized integers in C

D

dylanthomasfan

I want to know how one can use and manipulate byte-sized integers in C.
Byte-sized integears are useful where one uses them in arrays declared
in the heap, so it makes sense not to declare ints when we only need
bytes for flags, small counters etc. I know I could manipulate (char)
but that is too inelegant...
 
J

Jordan Abel

I want to know how one can use and manipulate byte-sized integers in C.
Byte-sized integears are useful where one uses them in arrays declared
in the heap, so it makes sense not to declare ints when we only need
bytes for flags, small counters etc. I know I could manipulate (char)
but that is too inelegant...

that's what char is for. the fact that it can also store letters doesn't
mean it's not fundamentally an int.

typedef signed char byte;
typedef unsigned char ubyte;
 
A

Anand

dylanthomasfan said:
I want to know how one can use and manipulate byte-sized integers in C.
Byte-sized integears are useful where one uses them in arrays declared
in the heap, so it makes sense not to declare ints when we only need
bytes for flags, small counters etc. I know I could manipulate (char)
but that is too inelegant...
Why would think that it's too inelegant.. as Jordan Abdel has already
put it.. "that's what it is there for".
The above could be reinforced if you refer the C99 std. (section 6.2.5)

"There are five standard signed integer types, designated as signed
char, short int, int, long int, and long long int."
 
M

Mike Wahler

dylanthomasfan said:
I want to know how one can use and manipulate byte-sized integers in C.

C offers three types for this: 'char', 'signed char', and 'unsigned char'.
Whether type 'char' is signed or not is implementation dependent, often
configurable. See your documentation for that.
Byte-sized integears are useful where one uses them in arrays declared
in the heap, so it makes sense not to declare ints when we only need
bytes for flags, small counters etc. I know I could manipulate (char)
but that is too inelegant...

How so?

-Mike
 
P

pete

dylanthomasfan said:
I want to know how one can use
and manipulate byte-sized integers in C.
Byte-sized integears are useful where one uses them in arrays declared
in the heap, so it makes sense not to declare ints when we only need
bytes for flags, small counters etc. I know I could manipulate (char)
but that is too inelegant...

The byte size integer types are:
1 unsigned char
2 signed char
3 char

My policy is not to use lower ranking ( than int ) integer types
without a special reason.
A string or an array would be a special reason.
Saving space would be a special reason,
but only if I were writing something for a platform
where it I knew that it was possible to save space that way,
and only if I needed the space.
It's possible and somewhat likely, that in a non array context,
that an object of type char, will be allocated on an int boundary,
and more or less treated as an int with the higher
addressed bytes masked off, which not only takes up as
much space as an int, but is slower.
On my machine, the following program, prints out "4".

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
char one, two;

printf("%d\n", (int)&one - (int)&two);
return 0;
}

/* END new.c */

Another thing about low ranking integer types,
is that they very frequently get promoted,
or otherwise converted, to type int, anyway,
in most arithmetic operations.
It's not uncommon for this coversion to be implemented
literaly in code translation.

If you really want to save space implementing flags,
use bits of an object of type unsigned.
For arithmetic with small numbers, type int is just fine.
 
K

Kenneth Brody

dylanthomasfan said:
I want to know how one can use and manipulate byte-sized integers in C.
Byte-sized integears are useful where one uses them in arrays declared
in the heap, so it makes sense not to declare ints when we only need
bytes for flags, small counters etc. I know I could manipulate (char)
but that is too inelegant...

Huh?

"I want to use byte-sized integers, but I don't want to use the one that
is supplied by the language because it's too inelegant."

Would this help?

typedef char byte_sized_integer;

You could then use the "byte_sized_integer" type for this purpose:

byte_sized_integer i, j, k;

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

Dave Vandervies

Kenneth Brody said:
Huh?

"I want to use byte-sized integers, but I don't want to use the one that
is supplied by the language because it's too inelegant."

Would this help?

typedef char byte_sized_integer;

You might want this instead:
typedef signed char byte_sized_integer;
typedef unsigned char byte_sized_unsigned;

You could then use the "byte_sized_integer" type for this purpose:

byte_sized_integer i, j, k;


dave
 
P

pete

Kenneth Brody wrote:
typedef char byte_sized_integer;

I don't like to read code like that.

I would not presume upon seeing "byte_sized_integer" in code
that it mean "char",
and I would have to look it up.
 
K

Kenneth Brody

pete said:
I don't like to read code like that.

I would not presume upon seeing "byte_sized_integer" in code
that it mean "char",
and I would have to look it up.

I guess my missing smiley made it less than obvious that I was being
facetious?

I'm still curious what the OP meant that chars would be "too inelegant".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
P

pete

Kenneth Brody wrote:
I guess my missing smiley made it less than obvious that I was being
facetious?

"In space, no one can hear you scream."

On Usenet, no one can read your sarcasm.
 
K

Keith Thompson

Kenneth Brody said:
I guess my missing smiley made it less than obvious that I was being
facetious?

I'm still curious what the OP meant that chars would be "too inelegant".

Well, the fact that the language overloads type char as both the type
used to hold characters and the type used to hold the smallest
addressible unit of memory is, IMHO, quite inelegant. But we're stuck
with it.
 
D

Dave Vandervies

Dave said:
Is that supposed to be funny TOO?

Sort of.

If you really want to follow his suggestion, my amendment improves it,
since the signedness of (unadorned) char isn't well-defined. But just
looking at it is enough to amplify the absurdity of the whole thing.


dave
 
M

Mike Wahler

Keith Thompson said:
Well, the fact that the language overloads type char as both the type
used to hold characters and the type used to hold the smallest
addressible unit of memory is, IMHO, quite inelegant. But we're stuck
with it.

Whoever decided upon codifying the term 'smallest addressible unit of
memory'
must be quite a character.

-Mike
 
K

Kenneth Brody

pete said:
"In space, no one can hear you scream."

On Usenet, no one can read your sarcasm.

On the internet, no one knows you're a dog.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,473
Latest member
pioneertraining

Latest Threads

Top