473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Byte-sized integers in C

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
16 1851
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
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

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.