473,395 Members | 1,936 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,395 software developers and data experts.

make this snippet efficient

KK
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);

Thank you.
KK

Jun 29 '06 #1
14 1828
KK wrote:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);


What's *INefficient* about it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '06 #2

Victor Bazarov wrote:
KK wrote:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);


What's *INefficient* about it?

V
--

Must I use reinterpret_cast operator ? How can I avoid it?

Jun 29 '06 #3
KK posted:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);

Thank you.
KK

You don't specify the amount of bits in a byte, however, looking at your
code, we can make an educated guess of 8.

You don't specify the amount of bytes in an int, however, looking at your
code, we can make an educated guess of 4.

You don't specify the byte order of the integer stored in the file, so we
can only hope that it's the same as the system's.

You don't specify the negative number system used to represent the number
in the file, so we can only hope that it's the same as the system's.

You don't specify whether the integer in the file contains padding bits, or
where they're located, nor do you specify whether the system stores
integers with padding bits, or where they're located.

Working with the scraps being given, try this:

unsigned Func( char (&array)[4] )
{
return reinterpret_cast<int&>( array );
}
--

Frederick Gotham
Jun 29 '06 #4
> > > unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
Must I use reinterpret_cast operator ? How can I avoid it?


use : unsigned char* byte = (unsigned char*)buff;

Jun 29 '06 #5
Frederick Gotham posted:

unsigned Func( char (&array)[4] )
{
return reinterpret_cast<int&>( array );
}

Should have casted to unsigned&, rather than int&.
--

Frederick Gotham
Jun 29 '06 #6
chandu wrote:
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);

Must I use reinterpret_cast operator ? How can I avoid it?


use : unsigned char* byte = (unsigned char*)buff;


Which means exactly the same thing without the keyword. How is it
any better?
Jun 29 '06 #7

Frederick Gotham wrote:
KK posted:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);

Thank you.
KK

You don't specify the amount of bits in a byte, however, looking at your
code, we can make an educated guess of 8.

When programming in C++, could one realistically expect to encounter a
system that does not have 8 bits in a byte?

Markus.

Jun 30 '06 #8
Markus Svilans posted:

When programming in C++, could one realistically expect to encounter a
system that does not have 8 bits in a byte?

You're on a Standard C++ newsgroup, and people here like to be pedantic. It
pays off in the long run, you end up with code that will run perfectly for
eons.

Here's a few things that the Standard allows:

(1) Machines need not use two's complement.
(2) Null pointers need not be all-bits-zero.
(3) Bytes need not be eight bits.
(4) Primitive types may contain padding bits.

Either you take all these things into account, and write FULLY-portable and
Standard-compliant code, or you don't.

If it ever got to a point where an old-fashioned constraint was hindering
efficiency or functionality, the constraint would be lifted. But until
then, you use the following macros to tell you how many bits you have in a
byte:
#define CHAR_BIT \
(((unsigned char)-1)/(((unsigned char)-1)%0x3fffffffL+1) \
/0x3fffffffL%0x3fffffffL*30+((unsigned char)-1)%0x3fffffffL \
/(((unsigned char)-1)%31+1)/31%31*5 + 4-12/(((unsigned char)\
-1)%31+3))

--

Frederick Gotham
Jun 30 '06 #9
Frederick Gotham wrote:
use the following macros to tell you how many bits you have in a
byte:
#define CHAR_BIT \
(((unsigned char)-1)/(((unsigned char)-1)%0x3fffffffL+1) \
/0x3fffffffL%0x3fffffffL*30+((unsigned char)-1)%0x3fffffffL \
/(((unsigned char)-1)%31+1)/31%31*5 + 4-12/(((unsigned char)\
-1)%31+3))


Why provide an implementation (especially one so... urk), rather than
just explain that this macro is available in the standard header
<climits>?

Luke

Jun 30 '06 #10
In article <11**********************@m73g2000cwd.googlegroups .com>,
ms******@gmail.com says...

[ ... ]
When programming in C++, could one realistically expect to encounter a
system that does not have 8 bits in a byte?


Yes. Under Windows CE, the smallest available type is 16 bits. A
number of DSPs don't have any 8-bit types either.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 30 '06 #11
Frederick Gotham wrote:
Markus Svilans posted:

When programming in C++, could one realistically expect to encounter a
system that does not have 8 bits in a byte?

You're on a Standard C++ newsgroup, and people here like to be pedantic. It
pays off in the long run, you end up with code that will run perfectly for
eons.


I can see your point. But in the last 10-15 years, has there been a new
CPU or microprocessor produced that does not have 8 bits in a byte? Are
there any C++ compilers that compile code for non-8-bit-byte systems?

I'm not arguing about the C++ standard, I'm just surprised that
variable byte sizes are something that people worry about enough to
include in the standard.

On second thought... 16-bit character sets could be considered to be
the harbingers of future non-8-bit bytes, could they not?
Here's a few things that the Standard allows:

(1) Machines need not use two's complement.
(2) Null pointers need not be all-bits-zero.
I'm confused. To set a pointer to null in C++, isn't the standard way
to do that to assign zero to the pointer? If you're on a system where
null pointers are non-zero, what happens to the pointer you thought you
had set to null?
From what you say, would the truly portable way to do that be to #define NULL depending on what system you're compiling for?
(3) Bytes need not be eight bits.
(4) Primitive types may contain padding bits.


I can see where padding bits would be necessary, for example
representing 32-bit integers on a 7-bit-per-byte system would require
five 7-bit bytes, with 3 padding bits. But are there any cases in
practice where primitive types actually contain padding bits?

Regards,
Markus.

Jun 30 '06 #12
Markus Svilans schrieb:
Here's a few things that the Standard allows:

(1) Machines need not use two's complement.
(2) Null pointers need not be all-bits-zero.


I'm confused. To set a pointer to null in C++, isn't the standard way
to do that to assign zero to the pointer? If you're on a system where
null pointers are non-zero, what happens to the pointer you thought you
had set to null?
From what you say, would the truly portable way to do that be to

#define NULL depending on what system you're compiling for?


An integer constant with value zero (eg. 0, 7+1-8, 0x0) is magically
converted to the systems null-pointer-value if assign to a pointer type.
(3) Bytes need not be eight bits.
(4) Primitive types may contain padding bits.


I can see where padding bits would be necessary, for example
representing 32-bit integers on a 7-bit-per-byte system would require
five 7-bit bytes, with 3 padding bits. But are there any cases in
practice where primitive types actually contain padding bits?


Don't know. But it would be a valid C++ system.

But only if the byte had 8 or more bits. A 7-bit-byte is not allowed.

Thomas
Jun 30 '06 #13
Markus Svilans posted:
You're on a Standard C++ newsgroup, and people here like to be
pedantic. It pays off in the long run, you end up with code that will
run perfectly for eons.
I can see your point. But in the last 10-15 years, has there been a
new CPU or microprocessor produced that does not have 8 bits in a
byte? Are there any C++ compilers that compile code for non-8-bit-byte
systems?

I'm not arguing about the C++ standard, I'm just surprised that
variable byte sizes are something that people worry about enough to
include in the standard.

On second thought... 16-bit character sets could be considered to be
the harbingers of future non-8-bit bytes, could they not?

Very possible. I think there's a certainty in life: Twenty years from
now, the world will have progressed more than we expected it, and in
unexpected ways.

Who's knows what the computers of tomorrow will bring?

Here's a few things that the Standard allows:

(1) Machines need not use two's complement.
(2) Null pointers need not be all-bits-zero.


I'm confused. To set a pointer to null in C++, isn't the standard way
to do that to assign zero to the pointer? If you're on a system where
null pointers are non-zero, what happens to the pointer you thought
you had set to null?

A "compile-time constant" is an expression whose value can be evaluated
at compile-time. Here's a few examples:

7

56 * 5 / 2 + 3

8 == 2 ? 1 : 6
If you have a compile-time constant which evaluates to zero, whether it
be:

0
5 - 5
2 * 6 - 12

Then it gets special treatment in C++, and qualifies as a null pointer
constant. A null pointer constant can be used to set a pointer to its
null pointer value, like so:

char *p = 0;

Because 0 qualifies as a null pointer constant, it gets special treatment
in the above statement (note how we'd normally have a type mismatch from
int to char*). Anyway, what the above statement does is set the pointer
to its null pointer value, whether that be:

0000 0000 0000 0000 0000 0000 0000 0000

or:

1111 1111 1111 1111 1111 1111 1111 1111

or:

1000 0000 0000 0000 0000 0000 0000 0000

or:

0000 0000 0000 0000 0000 0000 0000 0001

or:

1010 0101 1010 0101 1010 0101 1010 0101
From what you say, would the truly portable way to do that be to
#define NULL depending on what system you're compiling for?

No, all you do is:

char *p = 0;

And let your compiler deal with the rest.

(3) Bytes need not be eight bits.
(4) Primitive types may contain padding bits.


I can see where padding bits would be necessary, for example
representing 32-bit integers on a 7-bit-per-byte system would require
five 7-bit bytes, with 3 padding bits.

In actual fact it would make more sense to have a 35-Bit integer type,
instead of a 32-Bit one with padding.
But are there any cases in practice where primitive types actually
contain padding bits?

Mostly on supercomputers, I think.

Here's a quotation from a recent post on comp.lang.c:

For example, I'm currently logged into a system with the following
characteristics:

CHAR_BIT = 8
sizeof(short) = 8 (64 bits)
sizeof(int) = 8 (64 bits)
sizeof(long) = 8 (64 bits)

SHRT_MAX = 2147483647 (32 padding bits)
USHRT_MAX = 4294967295 (32 padding bits)

INT_MAX = 35184372088831 (18 padding bits)
UINT_MAX = 18446744073709551615 (no padding bits)

LONG_MAX = 9223372036854775807 (no padding bits)
ULONG_MAX = 18446744073709551615 (no padding bits)

(It's a Cray Y/MP EL running Unicos 9.0, basically an obsolete
supercomputer.)

--

Frederick Gotham
Jun 30 '06 #14

"Markus Svilans" <ms******@gmail.com> skrev i meddelandet
news:11*********************@75g2000cwc.googlegrou ps.com...
Frederick Gotham wrote:
(3) Bytes need not be eight bits.
(4) Primitive types may contain padding bits.


I can see where padding bits would be necessary, for example
representing 32-bit integers on a 7-bit-per-byte system would
require
five 7-bit bytes, with 3 padding bits. But are there any cases in
practice where primitive types actually contain padding bits?


No, but what we do have is machines with 36-bit integers and 9 bits
per byte.

http://www.unisys.com/products/clear...vers/index.htm

Should we not allow C++ to be implemented on such a machine?
A much more common problem is DSPs having 16 or 32 bit words as the
smallest unit. Then that will be the byte size, making sizeof(char) ==
sizeof(short) == sizeof(int). Quite possible!
Bo Persson
Jun 30 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Blankdraw | last post by:
The following 2 code segs seem to be the cause of my output infinite loop. Can someone tell me if this is wrong? I'd like to post the entire program, but that seems to distract everyone who migh...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
4
by: hufel | last post by:
Hi, I'm doing my first big project in C# and I'm stuck with a problem that I believe has a simple and efficient solution for it (I just haven't bumped into it yet...). The concept is the...
3
by: Jarod | last post by:
Hey I would like customize a little existing snippet in VS. Let's say there is snippet prop that works as a property. And it works pretty nice, but I would like it to automatically insert property...
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
12
by: pedagani | last post by:
Dear comp.lang.c++, Could you make this snippet more efficient? As you see I have too many variables introduced in the code. //Read set of integers from a file on line by line basis in a STL...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.