473,396 Members | 1,891 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,396 software developers and data experts.

using bits as flags


Wanted to create a 'flag byte' to use for a number of things. The following
seemed a reasonable approach:

#define connected = 0x01
unsigned char FlagByte;

why does this work:
FlagByte &= ~0x01; // set connected flag = 0

& this doesn't
FlagByte &= ~connected; // set connected flag = 0

Thanks,
Scott Kelley
Nov 14 '05 #1
5 1988
"Scott Kelley" <sc****@iccom.com> writes:
Wanted to create a 'flag byte' to use for a number of things. The following
seemed a reasonable approach:

#define connected = 0x01
This means that every occurrence of `connected' is replaced by `= 0x01'.
unsigned char FlagByte;

why does this work:
FlagByte &= ~0x01; // set connected flag = 0

& this doesn't
FlagByte &= ~connected; // set connected flag = 0


Think about what this becomes when `connected' is replaced (given what I
have written above), and you'll surely see what the mistake is. :)

Martin
Nov 14 '05 #2
Scott Kelley wrote:
Wanted to create a 'flag byte' to use for a number of things. The following
seemed a reasonable approach:

#define connected = 0x01

^^^

lose the bogus '='
--
Martin Ambuhl
Nov 14 '05 #3

"Scott Kelley" <sc****@iccom.com> wrote in message

#define connected = 0x01

you mean either

#define connected 0x01

or

int connected = 0x01;

Nov 14 '05 #4
"Scott Kelley" <sc****@iccom.com> wrote in message
news:aP********************@centurytel.net...

Wanted to create a 'flag byte' to use for a number of things. The following seemed a reasonable approach:

#define connected = 0x01


#define connected 0x01u

Note: bit manipulations with signed (and hence possibly negative) quantities
are potentially problematic in ISO C.

--
Peter
Nov 14 '05 #5

Thanks all

I was so focused on another area that I didn't see the obvious.

SK
Nov 14 '05 #6

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

Similar topics

16
by: Jacob H | last post by:
Hi there list, I'm a beginning programmer, so please correct me if any of the following assumptions are wrong. Suppose I have the decimal number 255. Since integers in Python are 32 bits, it...
3
by: MM | last post by:
Hi, What is the best structure/way to create an array of bits (actually true/false flags) of an arbitrary length ranging from about 20 upto about 500. Speed of access more of an issue than...
19
by: cppaddict | last post by:
Hi, I am going to have a series of bit flags which I could store in an array, or as a string ("10011001"), or any other way. I want to be able to turn this series of bits into an int. I know...
10
by: Ramprasad A Padmanabhan | last post by:
Hello all, On my linux box ( redhat 7.2 ), I have been using char as a boolean data type. In order to save on the number of bytes as compared to using int or short int. typedef char boolean;...
0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
3
by: Matthias S. | last post by:
Hi, I've got a class with an Options property. I'd like to implement this property in a way that a calling syntax like MyClassInstance.Options = MyClass.Option1 | MyClass.Option2; can be...
3
by: Bob Harrison | last post by:
I am monitoring some electronic equipment. Every few seconds i poll for equipment status. The results are returned in 6 groups - like 0A 40 08 00 00 00. Currently only the first 3 bytes contain...
13
by: Tomás | last post by:
The quantity of bits used by an unsigned integer type in memory can be determined by: typedef unsigned long long UIntType; CHAR_BIT * sizeof(UIntType) However, what would be a good...
6
by: =?Utf-8?B?U2FtZWVrc2hh?= | last post by:
Hi, I want to write a simple .net program to open a URL, fill in fields, and click on a button to submit it using .net 1.1 framework. Can someone help in suggesting the libraries I should use?...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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.