473,766 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

boolean logic question

In the book C++ Gothcas, Gotcha #7 is an example of using boolean logic to
simply code. My question follows this snippet from the book.

"Do you have to count to eight when presented with the following?"
int ctr = 0;
for (int i =0; i < 8; ++i) {
if (options & 1 << (8+i) )
if ( ctr++) {
cerr << "too many options selected";
break;
}

"Instead of this?"
typedef unsigned short Bits;
inline Bits repeated( Bits b, Bits m)
{ return b & m & (b & m) -1; }
//. . .
if ( repeated (options, 0XFF) )
cerr << "Too many options slected";

My Question: Why can't repeated() be written simply as
inline Bits repeated (Bits b, Bits m)
{ return b & m;}

Why is the "& (b & m) - 1" necessary? What is that all about?

Thanks,
Kurt


Jul 22 '05 #1
1 1824
Kurt Krueckeberg <re******@veriz on.net> wrote:
In the book C++ Gothcas, Gotcha #7 is an example of using boolean logic to
simply code. My question follows this snippet from the book.

"Do you have to count to eight when presented with the following?"
int ctr = 0;
for (int i =0; i < 8; ++i) {
if (options & 1 << (8+i) )
if ( ctr++) {
cerr << "too many options selected";
break;
}

"Instead of this?"
typedef unsigned short Bits;
inline Bits repeated( Bits b, Bits m)
{ return b & m & (b & m) -1; }
//. . .
if ( repeated (options, 0XFF) )
cerr << "Too many options slected";

My Question: Why can't repeated() be written simply as
inline Bits repeated (Bits b, Bits m)
{ return b & m;}

Why is the "& (b & m) - 1" necessary? What is that all about?

Thanks,
Kurt


With b & m you get a bitmask. If at least one bit is set, one bit will be
the highest set bit, for example: 0b00010000
If this is the only set bit then x-1 will be a mask with every bit up to
and including the highest set bit being 0 and every less significant bit
set. In this example: 0b00001111. Of course, a bitwise AND of those two
values will return 0.
On the other hand, if another bit was set, too (lets say 0b00010100), the
least significant set bit will be reset and all following bits are set,
resulting in the most significant bit staying set: 0b00010011.
Since the bit stays set, the bitwise AND won't reset this bit and the
result is non-zero.

HTH
Simon
--
Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 22 '05 #2

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

Similar topics

7
1712
by: Christopher Benson-Manica | last post by:
Given int g(); /* prototype */ int h(int); /* prototype */ void f( int x, int y ) /* x is either 0 or 1 */ { if( (/* other conditions */) && (/* other conditions */) && ( x == 1 || g() < 120 ) )
8
3266
by: Metro Sauper | last post by:
Why is the default text representation for booleans in C# 'True' and 'False' whereas in xml it is 'true' and 'false'? It seems like it would make sense to have them both the same. Metro T. Sauper, Jr. President Sauper Associates, Inc.
8
4856
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked one of these syntax formats to work; // if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
16
3253
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
76
4918
by: KimmoA | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the uppercase 'B' anyway?) and the header you can include (apparently) to get a real "bool". This isn't my point, however -- it should have been there from the beginning. char is a small int. We all know that. However, "char some_bool = 0;" simply...
2
5082
by: yarborg | last post by:
This is kind of a weird one and hard to find answers online because of the format of the question. Essentially I want to be able to have a string that looks like this "True AND True AND True" and evaluate that to a boolean which in this case would be True. Another example would be "(True or False) AND True" would be True. The reason for this is that I have a form where the user has to build some logic in a graphical interface. I can evaluate...
3
1259
by: Kevin Audleman | last post by:
I wrote a piece of boolean logic that doesn't work and I don't know why. The code should ONLY do something if the session variable is equal to 900123: if( ! $_SESSION == '900123') { //do some logic } However it always returns true. After staring at it for a while, I realized that the PHP way of doing things is to use the != operator,
2
1385
by: dpapathanasiou | last post by:
If I define a dictionary where one or more of the values is also a dictionary, e.g.: my_dict={"a":"string", "b":"string", "c":{"x":"0","y":"1"}, "d":"string"} How can I use the output of type() so I can do one thing if the value is a string, and another if the value is a dictionary? i.e., I'd like to define a loop like this, but I'm not sure of the
11
15013
by: KiranJyothi | last post by:
Hello All, I am writing a code in C where I have to use the Boolean data type. I am doing - typedef int flag; #define FALSE 0 #define TRUE 1 void main()
0
9571
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...
0
10009
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 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...
1
9959
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
9838
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...
1
7381
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
6651
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
5279
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.