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 1548
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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();
|
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,
|
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 =
|
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...
| |
by: arindam.mukerjee |
last post by:
I was running code like:
#include <stdio.h>
int main()
{
printf("%f\n", 9/5);
return 0;
}
|
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
|
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
|
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.
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |