473,748 Members | 8,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is this cast nessasary


Question

should this cast be necessary?
The Compiler is 8 Bits the ints are 16 bits.
The Code is for a ring buffer

It does not work without the cast in this compiler.
I have seen it work else where without it.
Which way is right (or should it matter at all?)

Thanks Neil



unsigned char t_in;
unsigned char t_out;

/*----------------------------------------------------------------*/
bit com_putchar (unsigned char c)
{

/*If the buffer is full, return an error value.*/
if ((TBUF_SIZE - (unsigned char)(t_in - t_out)) <= 2)
return (1);

/* More Code */
/* More Code */
/* More Code */
/* More Code */

return (0);
}

Oct 20 '06
10 1552
CBFalconer wrote:
Thad Smith wrote:
Neil wrote:
Thad Smith wrote:
Neil wrote:

should this cast be necessary?
The Compiler is 8 Bits the ints are 16 bits.
The Code is for a ring buffer

unsigned char t_in;
unsigned char t_out;

/*---------------------------------------------------------*/
bit com_putchar (unsigned char c)
{
/*If the buffer is full, return an error value.*/
if ((TBUF_SIZE - (unsigned char)(t_in - t_out)) <= 2)
return (1);

Assuming a standard implementation where t_in and t_out are input
and output indexes, the logic is incorrect after a buffer wrap.

You are right it is still wrong. I hate it when it looks like it
works, but is wrong) But it can be done continuous, not 2 cases.
The original two examples I used Use the full width of the type.

Assuming my buffer was 256 bytes with 8bit indexes I get a modulus
256 for free.

Since it is smaller I must truncate myself.

if ((TBUF_SIZE - ((t_in - t_out)%TBUF_SIZ E) <= 2)
... snip ...

Better, but still not correct in C. The result a%b has the same
sign as a if the results is non-zero. You can get what you want
by making it positive:
(t_in - t_out + TBUF_SIZE) % TBUF_SIZE

Caution. There is a subtle difference in the modulo operation on
negative values between C90 and C99. I am not sure exactly what,
but I know it is there. Check the standard(s).
C90 allows division to round to -inf, which always results in a
nonnegative remainder. On such an implementation, the change Thad Smith
suggested would be unnecessary but harmless.

Oct 23 '06 #11

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

Similar topics

22
3063
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20 3, NULL, 9, 82, 25
2
5838
by: Vilson farias | last post by:
Greetings, I was executing some queries when I discovered that 7.3.4's CAST is a little different from 7.1.2 when working with timestamps. When working with localized timestamps, I can't use CAST to convert a string for a timestamp anymore. Please take a look in the following scripts. #### PostgreSQL 7.1.2 #### bxs=# SELECT timeofday();
17
2675
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this cboFont_DrawItem event handler? Why does it need to be cast? -hazz ,................. cboFont.Items.AddRange(FontFamily.Families); } private void cboFont_DrawItem(object sender,
3
2290
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New DataSet ' Create connection Dim cn As New OleDbConnection With cn .connectionstring =
0
623
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is not valid.","Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> System.InvalidCastException: Specified...
17
2511
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
2
1333
by: mgsn | last post by:
I Have This this Query --------------------------------- set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER proc . @userId int=null, -- filter: user id. overrides custid when supplied @custId int=null, -- filter: customer id @vcsId int=null, -- filter: status
7
3951
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
3
3085
by: datosivakumar | last post by:
How to find out the space usage for all the databases in MS SQL thru SQL Query Analyzer? Normaly i used to view thru SQL Server Enterprise Manager Screen (view --> Taskpad) by selecting 1 db per view.
0
8991
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
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9374
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
9325
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
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
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...
0
4607
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
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
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.