473,803 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

which code runs faster?

I know I can use unsinged byte, but I need it for java, which code runs
faster in C?

int f1(char b) {
return (b&0x80)|(b&0x7 f);
}

or

int f2(char b) {
return b>0?b:256+bl
}

?

In my tests f2 performs better. I need a better opinion. :-)
Thanks.

Dec 12 '05
16 1525
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
nelu <ta********@gma il.com> wrote:
Walter Roberson wrote:
Or, if it is known that CHAR_BIT is 8, then return (int)(unsigned char)b; Thus, the designer must decide whether the intent is to get at the
native bit pattern, or to get at the value "as if" it were 2s complement.

I was wondering which is the best way to get the value of a byte
between 0 and 255.

I guess the safest way is to use the if function for what I need since
bit operations can have different results in that form from machine to
machine, right?


Suppose, though, that CHAR_BIT is not 8. Imagine, for example, that
the "Windows" key operated by setting the 9th bit. How is your function
defined in such a case?

"byte" is a general concept, but the size of a "byte" varies
with different architectures. Same with "char". "byte" and "char"
are not exactly synonyms either.


No, they're not synonyms, but a "byte" is by definition the size of
type "char" in C. (Of course the terms are used differently outside
the context of C.)

--
Keith Thompson (The_Other_Keit h) 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.
Dec 13 '05 #11
nelu wrote:
Keith Thompson wrote:
No, they're not synonyms, but a "byte" is by definition the size of
type "char" in C. (Of course the terms are used differently outside
the context of C.)
In Java byte (8 bits) is the equivalent of signed char.


No. In C, signed char needn't be two's complement. You should be trying
to
use int8_t, rather than signed char.
char in java has 2 bytes.

I have never used a machine that has more or less than 8 bits in 1 byte
(not even on Sinclair Spectrum).
Some of the older PDP implementations had non-8-bit byte sizes.
Can you tell me how they work?
Like any other implementation, just with wider than usual bytes.
Is it possible for them to comunicate with 8 bits bytes machines and if yes,
how?


The same way that 8-bit byte machines communicate through 7-pin din
connectors.
Any number of ways. But none of them are necessarily topical in
comp.lang.c.

--
Peter

Dec 13 '05 #12

Peter Nilsson wrote:
No. In C, signed char needn't be two's complement. You should be trying
to
use int8_t, rather than signed char.


True, I was thinking about char the way I used it on my machine :-).

Dec 13 '05 #13

Keith Thompson wrote:

No, they're not synonyms, but a "byte" is by definition the size of
type "char" in C. (Of course the terms are used differently outside
the context of C.)


In Java byte (8 bits) is the equivalent of signed char. char in java
has 2 bytes.
I have never used a machine that has more or less than 8 bits in 1 byte
(not even on Sinclair Spectrum). Can you tell me how they work? Is it
possible for them to comunicate with 8 bits bytes machines and if yes,
how?

Thanks

Dec 13 '05 #14
On 12 Dec 2005 15:09:08 -0800, in comp.lang.c , "nelu"
<ta********@gma il.com> wrote:
I was wondering which is the best way to get the value of a byte
between 0 and 255.


In C, a byte is the same size as a char. You get its value by
examining it. No casts are needed.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 13 '05 #15
Mark McIntyre <ma**********@s pamcop.net> writes:
On 12 Dec 2005 15:09:08 -0800, in comp.lang.c , "nelu"
<ta********@gma il.com> wrote:
I was wondering which is the best way to get the value of a byte
between 0 and 255.


In C, a byte is the same size as a char. You get its value by
examining it. No casts are needed.


Unless it's a signed char (or a plain char and plain char happens to
be signed); then you can cast it to unsigned char.

--
Keith Thompson (The_Other_Keit h) 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.
Dec 14 '05 #16
On 12 Dec 2005 20:22:02 -0800, "Peter Nilsson" <ai***@acay.com .au>
wrote:
nelu wrote:

<snip>
I have never used a machine that has more or less than 8 bits in 1 byte
(not even on Sinclair Spectrum).


Some of the older PDP implementations had non-8-bit byte sizes.

Terminology: this sounds like you mean different C implementations on
one "PDP" machine, or several "PDP" machines that implement a common
architecture. In fact Programmed Data Processor was the name used for
a series of machines by DEC (Digital Equipment Corporation) that were
mostly quite different, some with multiple implementations . There were
four models of PDP-10 namely KA-10, KI-10, KL-10, KS-10 which
implemented the same architecture, which was nearly the same as PDP-6.
There were several models of PDP-8 of which I recall 8/s, 8/i, 8/e,
8/m and 8/a, which implemented one architecture, nearly the same as
PDP-5 and similar to PDP-12 aka LINC-12, but very different from -6
and -10. PDP-7 and -9 were similar, and I believe also similar to -4
and -1, but different from -6/10 and -5/8/12. There were many
(numbered) models of PDP-11 like 11/20, 11/40, 11/45, 11/34, 11/70
etc. all implementing basically the same architecture, different from
all other PDP series machines.

There were also quite a few non-DEC machines with non-8-bit bytes.
Can you tell me how they work?


Like any other implementation, just with wider than usual bytes.
Is it possible for them to comunicate with 8 bits bytes machines and if yes,
how?


The same way that 8-bit byte machines communicate through 7-pin din
connectors.
Any number of ways. But none of them are necessarily topical in
comp.lang.c.


In the early days of the Internet and before that ARPANET, this was a
significant issue and there are a number of features (still) specified
especially in FTP to deal with data exchange with systems having a
byte other than 8 bits, no longer used today except perhaps
occasionally by mistake.

- David.Thompson1 at worldnet.att.ne t
Dec 19 '05 #17

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

Similar topics

10
1716
by: Vineet | last post by:
If anybody has any insight into this problem I'm running into I would really appreciate if you could write to me... I'm running a simple C++ program on Solaris 8 that forks and execs a bunch of processes. It's been running fine for years, but now that I've moved to faster hardware, I'm running into a problem that surfaces more frequently as the hardware I'm using gets better/faster -- it seems like some sort of race condition issue. ...
22
1923
by: The Doctor | last post by:
Hi all, Recently I was asked the question about whether ++U or U++ is faster if U is a user defined type. What's the answer? Thank you
65
12632
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second method was a piece of C code which turned out to be incorrect and incomplete but by modifieing it would still be usuable. The first method was this piece of text:
16
1853
by: John Salerno | last post by:
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '<p>' + paragraph + '</p>\n\n' vs. def p(self, paragraph):
25
1724
by: Ganesh | last post by:
Hi, This is a question that pertains to pointers in general (C or C++). Which of the following is faster and why? for (int i = 0; i < N; i++) = ... a... (or)
16
5672
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
17
1976
by: onkar | last post by:
which one runs faster ?? for(i=0;i<100;i++) for(j=0;j<10;j++) a=0; OR for(j=0;j<10;j++) for(i=0;i<100;i++)
23
2537
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80) return s def descrambleLine(line):
84
3987
by: Patient Guy | last post by:
Which is the better approach in working with Javascript? 1. Server side processing: Web server gets form input, runs it into the Javascript module, and PHP collects the output for document prep. 2. Client side processing: Web server gets form input and passes it to PHP which includes the Javascript written in a way to make the form input processed on the client side and rendered (probably using DOM function calls) on that side as...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.