473,320 Members | 2,083 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,320 software developers and data experts.

Setting bits to on and off

Hi. This might sound dumb, but I need some guidence. I need to know how to
set bits to 1 and to 0, on and off. Thus far I can set a single bit to 1 by
using LSH and bitwise OR operators, but can't figure out how to set a single
bit to 0. Please give me some ideas on how to turn on and off bits. Thank
you.


Apr 29 '06 #1
3 5251
* 4600cc:
Hi. This might sound dumb, but I need some guidence. I need to know how to
set bits to 1 and to 0, on and off. Thus far I can set a single bit to 1 by
using LSH and bitwise OR operators, but can't figure out how to set a single
bit to 0. Please give me some ideas on how to turn on and off bits. Thank
you.


Try

#include <iostream>
#include <ostream>
#include <bitset>

int main()
{
std::bitset<32> bitset;

std::cout << bitset << std::endl;
bitset.at( 5 ) = 1;
std::cout << bitset << std::endl;
bitset.flip();
std::cout << bitset << std::endl;
bitset.at( 1 ) = 0;
std::cout << bitset << std::endl;
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 29 '06 #2
On Sat, 29 Apr 2006 10:16:05 -0400, "4600cc" <no****@nosite.org> wrote
in comp.lang.c++:
Hi. This might sound dumb, but I need some guidence. I need to know how to
set bits to 1 and to 0, on and off. Thus far I can set a single bit to 1 by
using LSH and bitwise OR operators, but can't figure out how to set a single
bit to 0. Please give me some ideas on how to turn on and off bits. Thank
you.


Try this:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
unsigned int target = 0;

for (int mask = 0; mask < 8; ++mask)
{
target |= (1U << mask);
cout << "target with bit " << mask << " set = 0x"
<< setw(2) << setfill('0') << hex << target << '\n';
target &= ~(1U << mask);
cout << "target with bit " << mask << " cleared = 0x"
<< setw(2) << setfill('0') << hex << target << '\n';
}
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Apr 29 '06 #3
4600cc wrote:
Hi. This might sound dumb, but I need some guidence. I need to know how to
set bits to 1 and to 0, on and off. Thus far I can set a single bit to 1 by
using LSH and bitwise OR operators, but can't figure out how to set a single
bit to 0. Please give me some ideas on how to turn on and off bits. Thank
you.


(Note: Example code below stresses clarity over efficiency. For best
results, use std::bitset when possible.)

The following tautologies may help you. If b is a single bit, then:
(1a) b | 1 == 1
(1b) b | 0 == b
(2a) b & 1 == b
(2b) b & 0 == 0
(3a) b ^ 1 == ~b
(3b) b ^ 0 == b

(Note: & means AND, | means OR, ^ means XOR.)

There are two steps to manipulating a bit within a number. First,
define a "mask". Second, apply that mask to your number using some
bitwise operation.

The tasks we are usually interested in are: setting a bit, unsetting a
bit, flipping a bit, testing the value of a bit.
Setting a bit:
We will use bitwise OR (|) to accomplish this task. (1a) tells us that
the bit(s) we would like set to 1 should have a 1 in the corresponding
position in our mask. (1b) tells us that placing a 0 in each of the
mask's other bit positions will leave the original bit unchanged.

Example:
unsigned set(unsigned x, unsigned n)
{
unsigned mask = 1 << n ;
return (x | mask) ;
}
Unsetting a bit:
We will use bitwise AND (&) to accomplish this task. (2b) tells us that
the bit(s) we would like unset should have a 0 in the corresponding
position in our mask. (2a) tells us that placing a 1 in each of the
mask's other bit positions will leave the original bit unchanged. Note
the mask required to unset a bit is the bitwise NOT(~) of the mask
required to set a bit.

Example:
unsigned unset(unsigned x, unsigned n)
{
unsigned mask = ~(1 << n) ;
return (x & mask) ;
}
Flipping a bit:
We will use bitwise XOR (^) to accomplish this task. (3a) tells us that
the bit(s) we would like flipped should have a 1 in the corresponding
position in our mask. (3b) tells us that placing a 0 in each of the
mask's other bit positions will leave the original bit unchanged.

Example:
unsigned flip(unsigned x, unsigned n)
{
unsigned mask = 1 << n ;
return (x ^ mask) ;
}
Testing the value of a bit:
We will use bitwise AND (&) to accomplish this task. We will place a 1
in the mask in the position we wish to check, and 0 in the remaining
positions. Note that by (2b), all positions in which we are not
interested will be forced to 0. By (2a), the position we are interested
in will remain unchanged. Therefore, if the bit is not set, the result
will be all 0s.

Example:
bool is_set(unsigned x, unsigned n)
{
unsigned mask = 1 << n ;
return (x & mask) ? true : false ;
}
--
Alan Johnson
Apr 29 '06 #4

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

Similar topics

3
by: JKop | last post by:
Consider the following platform: char = 8-bit short = 16-bit int = 32-bit long = 32-bit
35
by: Scott Kelley | last post by:
Is there a way to reset all elements of an array with a single instruction? I want to set all elements to zero. Currently looping to do so. thx, Scott Kelley
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
6
by: Peter Krikelis | last post by:
Hi All, I am having a problem setting up input mode for serial communications. (Sorry about the long code post). The following code is what I use to set up my comm port.
1
by: James Dean | last post by:
I set the first 4 index values of the colorpalette to the following: 0 = white; 1 = red; 2 = blue; 3 = OverlapColor(mix of red and blue); So the first 4 bits will be used to to set one...
5
by: verrice | last post by:
Hello, I'm writing a web application where I want to load bits of data at runtime from the client end (via AJAX). The loading of the data part works great, but for some reason anytime I try to...
1
by: nasima khan | last post by:
Hi, i am nasima. I have got a code for setting the screen resolution of my page, but i am unable to understand. Can any one give a complete data explanation of the below code. Sub ChangeRes(X...
4
by: Nikos Hatzigiannakis | last post by:
The following code does not display the number 10 in hex format. The hex flag doesn't seem to work. Any ideas? Compiler is DEV C++ 4.9.9.2 http://www.bloodshed.net/
2
by: Ramesh | last post by:
Hi I have a structure as below on big endian based system typedef struct { unsigned long LedA:5; unsigned long LedB:4; unsigned long LedC:8;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.