The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bitwise and if Puntino High Level Programming 3 04-28-2008 10:22 AM
If with set operators Khoomfire UNIX for Advanced & Expert Users 2 04-25-2008 08:03 AM
Bitwise negation dLloydm Shell Programming and Scripting 3 05-17-2007 10:57 AM
resetting counter using bitwise XOR mrgubbala High Level Programming 1 09-14-2006 01:58 AM
bitwise operators areef4u UNIX for Advanced & Expert Users 9 08-04-2006 05:39 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2002
Registered User
 

Join Date: Mar 2002
Location: Greece
Posts: 14
Stumble this Post!
Bit-fields and Bitwise operators

Hi,
Is it possible to use bitwise operators in bit fields?
For example:

typedef struct Mystruct {
unsigned char A :1 ;
unsigned char B :1 ;
} Mystruct;

and assume

struct Mystruct STR_1S, STR_2S, tempSTRS = {0};

then the following line:

tempSTRS = STR_1S & STR_2S;
gives the error:

operants must have integral type: op "&"

One way to overcome the problem is to perform the bitwise operations in each member separelty i.e.
tempSTRS.A = STR_1S.A & STR_2S.A;
tempSTRS.B = STR_1S.B & STR_2S.B;

Is there any other more eficient way?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-23-2002
killerserv's Avatar
Unix Predator
 

Join Date: Dec 2000
Location: Phoenix, AZ
Posts: 670
Stumble this Post!
Looks more like homework.. ?
If it is hope you did check the Forum rules. No Homework/Assignments allowed.
Reply With Quote
  #3 (permalink)  
Old 04-23-2002
Registered User
 

Join Date: Mar 2002
Location: Greece
Posts: 14
Stumble this Post!
No it's not a homework.

I am a software engineer for the last for years
Reply With Quote
  #4 (permalink)  
Old 04-23-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,427
Stumble this Post!
Bit fields are like small integers. You can do everything to them that you can do to an integer including the bit-wise operators. The only exception that I can think of is that you cannot take their address.

Putting a bunch of integers into a struct would never turn that struct into an integer. The same is true for bit fields.

If you want efficient, stay away from bit fields, They are a hack to save space at the expense of execution speed. They made sense when you had small ints and limited memory.

But from your code, you don't have small ints, you have bits. If you just want to turn bits on and off and test them for being on and off, well, just do that. It's easy with the bit-wise ops. And if you turn bits on and off in an int, then you do want you wanted to do.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0