473,406 Members | 2,847 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.

BItwise Operation ...

Hello, all.

I am not good at bitwise operation, so I wonder if some experts could
do me a help.

In a.c, somebody defined
================================================
# define u_int64 unsigned long long
# define u_short unsigned short
# define u_int unsigned int

u_short txid_slt;
u_int txid_sqn;

u_int64 tx_id= ((u_int64)txid_slt << 32) | (u_int64)txid_sqn;

Later, in b.c, I have to reassemble tx_id into "slt" and "sqn".
=================================================
struct redo_xid {
u_int sqn;
u_short slot;
};

Can I use the following expression to achieve this goal?
==================================================
struct redo_xid xid = (struct redo_xid)tx_id;

Many many thanks!

Oct 30 '07 #1
4 2314
On 30 Oct, 09:49, loudking <loudk...@gmail.comwrote:
I am not good at bitwise operation,
they aren't that complicated...

so I wonder if some experts could
do me a help.

In a.c, somebody defined
================================================
# define u_int64 unsigned long long
long long doesn't have to be 64-bits

# define u_short unsigned short
# define u_int unsigned int
you appear to be assuming int is 32 bits. It doesn't have to be.
I generally don't like typedefs like this.

u_short txid_slt;
u_int txid_sqn;

u_int64 tx_id= ((u_int64)txid_slt << 32) | (u_int64)txid_sqn;
I wouldn't bother with the casts.

u_int64 tx_id = (txid_slt << 32) | txid_sqn;

Later, in b.c, I have to reassemble tx_id into "slt" and "sqn".
=================================================
struct redo_xid {
u_int sqn;
u_short slot;

};

Can I use the following expression to achieve this goal?
==================================================
struct redo_xid xid = (struct redo_xid)tx_id;
why not reverse the previous step?

struct redo_xid xid;

xid.sqn = tx_id & 0xffffffff; /* 32 1s */
xid.slot = (tx_id >32) & 0xffff;
--
Nick Keighley

Casting is almost always wrong,
and the places where it's right are rarely the places you'd guess.
Richard Heathfield

Oct 30 '07 #2
why not reverse the previous step?
>
struct redo_xid xid;

xid.sqn = tx_id & 0xffffffff; /* 32 1s */
xid.slot = (tx_id >32) & 0xffff;

--
Thanks!

Oct 30 '07 #3
loudking:
# define u_int64 unsigned long long
# define u_short unsigned short
# define u_int unsigned int

C has typedef for a reason:

typedef long long unsigned u_int64;
typedef short unsigned u_short;
typedef unsigned u_int;

The syntax for typedef is the same for defining a variable:

int (*x)[7]; /* x is a variable */
typedef int (*x)[7]; /* x is a type */
You'll notice the difference when you try:

typedef int *Type1;
#define Type2 int*

int main(void)
{
const Type1 a;
const Type2 b;

b = 0;

return 0;
}
u_short txid_slt;
u_int txid_sqn;

u_int64 tx_id= ((u_int64)txid_slt << 32) | (u_int64)txid_sqn;

Later, in b.c, I have to reassemble tx_id into "slt" and "sqn".
=================================================
struct redo_xid {
u_int sqn;
u_short slot;

};

Can I use the following expression to achieve this goal?
==================================================
struct redo_xid xid = (struct redo_xid)tx_id;

None of the C Standards say that you can... in fact they explicitly
allow an implementation to explode in your face if you try. As for
*your own* platform... who knows...

Martin

Oct 30 '07 #4
Martin Wells <wa****@eircom.netwrites:
loudking:
># define u_int64 unsigned long long
# define u_short unsigned short
# define u_int unsigned int

C has typedef for a reason:

typedef long long unsigned u_int64;
typedef short unsigned u_short;
typedef unsigned u_int;
[...]

Agreed. C also has "unsigned short" and "unsigned int" for a reason.
Just refer to "unsigned short" and "unsigned int" directly. The
aliases "u_short" and "u_int" add nothing useful; they just make me
wonder why you bothered to define new names for types that already
have perfectly good names -- or, worse, whether you've defined
"u_short" as something other than "unsigned short".

As for u_int64, unsigned long long is required to be *at least* 64
bits, but it's allowed to be wider.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 30 '07 #5

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

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
2
by: Joe Gonzalez | last post by:
Is it possible to perform bitwise operations in selects in DB2 8.1? In postgres I can say something like SELECT ... FROM <sometable> WHERE RecFlags && 0x08 <> 0 to get a bitwise and operation. I...
7
by: Jerry | last post by:
I want an algorithm that do arithmetic operations(divide,mutiply,add etc.)just using bitwise operators:<<,>>,&,|,^; For example,how "a/10" can be implemented. I just want a hint. Thanks.
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
10
by: David R. | last post by:
I want to do bitwise operation on some large integers. For example, Response.Write CBool(2 AND 2^30) ' returns False Response.Write CBool(2 AND 2^31) ' CRASHED! Looks like the AND...
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
3
by: Sakhtkoosh | last post by:
I have written a program in C environment. I need to do operation on codes with more than 64 elements whose values are 0 and 1. At first, I simulated it with an array in 2 dimension(n*m) but after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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
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.