By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

Byte-sized integers in C

P: n/a
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...

Nov 17 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On 2005-11-17, dylanthomasfan <So***************@gmail.com> wrote:
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;
Nov 17 '05 #2

P: n/a
dylanthomasfan wrote:
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."
Nov 17 '05 #3

P: n/a

"dylanthomasfan" <So***************@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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
Nov 17 '05 #4

P: n/a
dylanthomasfan wrote:

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.

--
pete
Nov 17 '05 #5

P: n/a
dylanthomasfan wrote:

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:Th*************@gmail.com>

Nov 17 '05 #6

P: n/a
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.

--
pete
Nov 17 '05 #7

P: n/a
In article <43***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
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

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Note that non-portability of the translated code is not a requirement of
the C Standard.
--Richard Heathfield in comp.lang.c
Nov 17 '05 #8

P: n/a
pete wrote:

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.


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:Th*************@gmail.com>

Nov 17 '05 #9

P: n/a
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.

--
pete
Nov 17 '05 #10

P: n/a
Dave Vandervies wrote:

In article <43***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:

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;


Is that supposed to be funny TOO?

--
pete
Nov 17 '05 #11

P: n/a
Kenneth Brody <ke******@spamcop.net> writes:
pete wrote:
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 17 '05 #12

P: n/a
In article <43***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
Dave Vandervies wrote:

In article <43***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:

>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;


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

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Since I don't take you to be that kind of idiot, I assumed you were working
from an actual pre-existing specification that you could cite, rather than
making things up on the spot. --Chris Dollin in comp.lang.c
Nov 17 '05 #13

P: n/a
pete <pf*****@mindspring.com> writes:
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.


Yeah, right.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 17 '05 #14

P: n/a

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Kenneth Brody <ke******@spamcop.net> writes:
pete wrote:
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.


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.


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

-Mike
Nov 18 '05 #15

P: n/a
pete wrote:

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.


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:Th*************@gmail.com>
Nov 19 '05 #16

P: n/a
Kenneth Brody wrote:
pete wrote:
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.

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

If you're a dog, everybody knows. Except maybe you.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 19 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.