473,609 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing Data to Bit Field

Hi everybody!

Is it possible to write a value into a bit field which initializes all
bits?

example:

struct Field
{
unsigned int Bit1 : 1;
unsigned int Bit2 : 2;
} MyFied;

Now, I would like to write the value 3 to the variable MyField,
so that Bit1 and Bit2 become TRUE ( == 1).

Any suggestion?

Thanks!

Feb 27 '06 #1
31 3034

Zero wrote:
Hi everybody!

Is it possible to write a value into a bit field which initializes all
bits?

example:

struct Field
{
unsigned int Bit1 : 1;
unsigned int Bit2 : 2;
I guess you mean `: 1` here as well (judging by the rest of the post).
As it is, Bit2 has width 2, not 1.
} MyFied;

Now, I would like to write the value 3 to the variable MyField,
so that Bit1 and Bit2 become TRUE ( == 1).

Any suggestion?


AFAIK, not in a portable way.

The closest you can portably get is to have a constant struct with Bit1
and Bit2 set to 1, and then assign that to variables of the same type.
E.g.:

struct Field all_bits_one = { 1, 1};

MyFied = all_bits_one;

If you don't want to be portable, you can find out how your
implementation represents integers (or similar handy objects) and try
constructing them appropriatelly and casting to your struct.

--
BR, Vladimir

Feb 27 '06 #2

Vladimir S. Oka wrote:
Zero wrote:
Hi everybody!

Is it possible to write a value into a bit field which initializes all
bits?

example:

struct Field
{
unsigned int Bit1 : 1;
unsigned int Bit2 : 2;
I guess you mean `: 1` here as well (judging by the rest of the post).
As it is, Bit2 has width 2, not 1.
} MyFied;

Now, I would like to write the value 3 to the variable MyField,
so that Bit1 and Bit2 become TRUE ( == 1).

Any suggestion?


AFAIK, not in a portable way.

The closest you can portably get is to have a constant struct with Bit1
and Bit2 set to 1, and then assign that to variables of the same type.
E.g.:

struct Field all_bits_one = { 1, 1};


Obviously:

const struct Field all_bits_one = { 1, 1};

if you wanted it constant as I suggested.

MyFied = all_bits_one;

If you don't want to be portable, you can find out how your
implementation represents integers (or similar handy objects) and try
constructing them appropriatelly and casting to your struct.

--
BR, Vladimir


Feb 27 '06 #3
You were right, it must have been unsigned int Bit2 : 1;

Feb 27 '06 #4
Can you give me an example for the cast-operation?

Feb 27 '06 #5

Zero wrote:
Can you give me an example for the cast-operation?


Please quote what, and who you're replying to. If you're using Google,
you need to click on Show Options, and then Reply that appears beneath
the header (and while you're at it, don't top-post either in case you
feel tempted).

I's also move to retract my suggestion that you can cast to a struct.
Casts only work on scalar types, and structs are not scalar types. I
blame insuficient coffee intake. Sorry.

Why would you want to do such a thing anyway?

--
BR, Vladimir

Feb 27 '06 #6

Vladimir S. Oka schrieb:
Zero wrote:
Can you give me an example for the cast-operation?
Please quote what, and who you're replying to. If you're using Google,
you need to click on Show Options, and then Reply that appears beneath
the header (and while you're at it, don't top-post either in case you
feel tempted).


Ok, I never have quoted before...
I's also move to retract my suggestion that you can cast to a struct.
Casts only work on scalar types, and structs are not scalar types. I
blame insuficient coffee intake. Sorry.
Yes, that is also what my compiler says...
Why would you want to do such a thing anyway?


It would be the fastest way to initialize the bits, wouldn't it?

Zero

Feb 27 '06 #7
Zero wrote:
Vladimir S. Oka schrieb:
Zero wrote:
Can you give me an example for the cast-operation?


Please quote what, and who you're replying to. If you're using Google,
you need to click on Show Options, and then Reply that appears beneath
the header (and while you're at it, don't top-post either in case you
feel tempted).


Ok, I never have quoted before...
I's also move to retract my suggestion that you can cast to a struct.
Casts only work on scalar types, and structs are not scalar types. I
blame insuficient coffee intake. Sorry.


Yes, that is also what my compiler says...
Why would you want to do such a thing anyway?


It would be the fastest way to initialize the bits, wouldn't it?


It shouldn't be less efficient than what I suggested earlier (a good
compiler will probably behind the scenes effectively do what you were
trying to). It's also much more readable.

You should also know the first rule of optimisation:

1. Don't do it.

Compilers are generally better at it. Second rule is useful as well:

2. Don't do it yet.

--
BR, Vladimir

Feb 27 '06 #8
On 2006-02-27, Zero <ch********@web .de> wrote:
snip Yet another tedious reminder about how to reply.
Doesn't worry me too much since if I'm interested in a thread then it
can easily be tagged or reconstructed at will despite being an online
newsreader. Personally I hate people constantly including the entire
previous post : the title and thread ID is usually enough for me to
know where I am. Still, horses for courses : and *some* context is
often nice especially in a "to and fro" type thread.

I's also move to retract my suggestion that you can cast to a struct.
Casts only work on scalar types, and structs are not scalar types. I
blame insuficient coffee intake. Sorry.


Yes, that is also what my compiler says...
Why would you want to do such a thing anyway?


It would be the fastest way to initialize the bits, wouldn't it?

Zero


Maybe. What About using a Union? BTW its horrible : address the fields
seperately and let the compiler take care of optimizations unless
there really is a need to write data to a word at exactly the same time.
--
Remove evomer to reply
Feb 27 '06 #9

Richard G. Riley schrieb:
Maybe. What About using a Union?


What do you mean with your hint?

Feb 27 '06 #10

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

Similar topics

6
5626
by: DanielEKFA | last post by:
Hey there :) I was once told that the STL classes had member functions to write their data to disk and to restore that data. Searching google (and why are there no "stl" or "map" manpages?), it seems like someone was pulling my leg, because I really can't find anything about it. So, was I misinformed? Or perhaps this person was talking about some other classes you could use to do such a thing?
6
18832
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at the data in the excel file that if the first character in the excel file cell is numeric it will read and write only numeric values only. If I sort the coloumn in the excel file and the first character in the cell read is alphanumeric then only...
9
1959
by: curious_one | last post by:
All, I have a struct struct { char a; char b; }some_struct; I have a shared memory that can contain 16bit wide data, I find that when writing an 8bit value in to char "a" the same value is over-written onto "b".
0
1643
by: Richard Marsden | last post by:
I'm having a lot of trouble writing large chunks of binary data (tests are in the range of 16-512K, but we need support for large longblobs) to MySQL using ODBC. Database is local on a W2K system, but I have to support all modern Windows systems, and a variety of ODBC configurations. (I'll be testing against multiple ODBC databases soon - but development is against MySQL) I've been able to adapt some example code that executes a...
8
3440
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way I access the array elements. Thanks for your help. #include <stdio.h>
0
2414
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical price of a stock. The program will allow users to query the stock price of a particular date and output its statistics (mean price, median price and standard deviation) for any specified period. Your program must be menu driven and works as follows.
3
1879
by: Bidhan | last post by:
Hi, I have a table (Stock) important field are partsNo., des, qty, pPrice, sPrice, qtyBuy. I make a query based on the table and has one more field SAB(qty+qtyBuy). I made a form based on the query. User can enter data in any field. When I enter data in ‘qtyBuy’ field I can see the added value by SAB field. My obligation is to set this value into stock table in qty field. To perform this action I run an action query in a macro in qty field...
1
1643
by: ChrisFrohlich | last post by:
ASP.NET 2.0 with Text DataTypes: I've got a similar question going in the SQL group, but I was wondering if anyone has successfully implemented reading/writing character data from a Text datatype column in SQL 2000. I know there's a Varchar(MAX) feature in SQL2005, but my budget mocks me right now and I need to do it in SQL 2K. Situation: I'm building a resume DB, and for ease of input, I'd really like to be able to use a single...
3
2628
by: Thorben Grosser | last post by:
Hello Newsgroup, I am doing some archive database and therefore got one table indexing every folder and one table storing which rack belongs to which department, eg: table folders : +-----------+------+-------+ | folder_id | rack | date |
30
2676
by: Cramer | last post by:
I've finally gotton board with TDD (test driven development) and wow is it effective! I went from sceptic to True Believer with my first effort. My question: According to the various books and articles I have read about TDD, a good unit test does not rely on the database or other such external/environmental conditions. More generally, a good unit test is atomic and makes as few assumptions about its runtime environment as possible. But...
0
8145
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...
1
8236
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
8410
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
7030
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
6068
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
5526
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4037
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...
1
1690
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.