better way of doing this!

H

Hameed U. Khan

Hi
I've started learning C++. I want to know is there any better way of
solving the problem defined below or how can I improve the bitwise
operations. If there is any other method using bitwise operators I'll
love to know. Thanks

// Exercise 3.2
/* Object:
Creat a program that prompts the user to input an integer in decimal
form. Then, invert the last bit of its binary representation.
That is, if the last bit is 1, then change it to 0, and vice versa.
The result should then be displayer as a decimal number. How does
the adjustment affect the resulting integer value?
(Hint: use a bitwise operator.)
*/

#include <iostream>

using namespace std;

int main()
{
unsigned short num = 0;
unsigned short mask = 1;
unsigned short last_bit = 0;


cout <<"Enter number: ";
cin >> num;

last_bit = num & mask; // Extracts last bit
last_bit = ~last_bit&mask; // Inverts extracted bit
num = (num&~mask)^last_bit;


cout << "Num: "
<< num
<<endl;
return 0;
}


Hameed U. Khan
 
N

Noah Roberts

Hameed said:
Hi
I've started learning C++. I want to know is there any better way of
solving the problem defined below or how can I improve the bitwise
operations. If there is any other method using bitwise operators I'll
love to know. Thanks

// Exercise 3.2
/* Object:
Creat a program that prompts the user to input an integer in decimal
form. Then, invert the last bit of its binary representation.
That is, if the last bit is 1, then change it to 0, and vice versa.
The result should then be displayer as a decimal number. How does
the adjustment affect the resulting integer value?
(Hint: use a bitwise operator.)
*/

#include <iostream>

using namespace std;

int main()
{
unsigned short num = 0;
unsigned short mask = 1;
unsigned short last_bit = 0;


cout <<"Enter number: ";
cin >> num;

last_bit = num & mask; // Extracts last bit
last_bit = ~last_bit&mask; // Inverts extracted bit
num = (num&~mask)^last_bit;

I think you really meant (num & ~mask) | last_bit here but that isn't
necissary either. You can use the xor operator directly on num without
the fancy in between. I'll leave how to do it as an excercize...the
entire flip last bit operation is a single xor and assignment.
 
H

Howard

Hameed U. Khan said:
Hi
I've started learning C++. I want to know is there any better way of
solving the problem defined below or how can I improve the bitwise
operations. If there is any other method using bitwise operators I'll
love to know. Thanks

// Exercise 3.2
/* Object:
Creat a program that prompts the user to input an integer in decimal
form. Then, invert the last bit of its binary representation.
That is, if the last bit is 1, then change it to 0, and vice versa.
The result should then be displayer as a decimal number. How does
the adjustment affect the resulting integer value?
(Hint: use a bitwise operator.)
*/

hint:

Using just a single statement...

: the or-and-assign operator (a |= b) will turn on any bit set to 1 in the
mask.
: the and-and-assign operator (a &= b) will turn off any bit set to 0 in the
mask.
: the xor-and-assign operator (a ^= b) will reverse the value of any bits
set to 1 in the mask.

Now, you pick the one you need...

Got it? Good. :)

-Howard
 
H

Howard

Howard said:
hint:

Using just a single statement...

: the or-and-assign operator (a |= b) will turn on any bit set to 1 in the
mask.
: the and-and-assign operator (a &= b) will turn off any bit set to 0 in
the mask.
: the xor-and-assign operator (a ^= b) will reverse the value of any bits
set to 1 in the mask.

Those all should probably read "... any bits in a, which are set to
[whatever] in b".

-H
 
V

Victor Bazarov

Hameed said:
I've started learning C++. I want to know is there any better way of
solving the problem defined below or how can I improve the bitwise
operations. If there is any other method using bitwise operators I'll
love to know. Thanks

// Exercise 3.2
/* Object:
Creat a program that prompts the user to input an integer in decimal
form. Then, invert the last bit of its binary representation.
That is, if the last bit is 1, then change it to 0, and vice versa.
The result should then be displayer as a decimal number. How does
the adjustment affect the resulting integer value?
(Hint: use a bitwise operator.)
*/

#include <iostream>

using namespace std;

int main()
{
unsigned short num = 0;
unsigned short mask = 1;
unsigned short last_bit = 0;


cout <<"Enter number: ";
cin >> num;

last_bit = num & mask; // Extracts last bit

"Last" is a bad title. It's actually "least significant".
last_bit = ~last_bit&mask; // Inverts extracted bit
num = (num&~mask)^last_bit;

This is way too complicated. *All* you needed to do was

num = num ^ mask;

It would flip (invert) the bits that have 1 in the mask and leave
alone those that have 0 in the mask.
cout << "Num: "
<< num
<<endl;
return 0;
}


Hameed U. Khan

V
 
F

Frederick Gotham

Hameed U. Khan posted:
// Exercise 3.2
/* Object:
Creat a program that prompts the user to input an integer in decimal
form.


(This imposes no restriction on the whether the integer be positive or
negative.)

The integer type isn't specified, so I suppose a guess would be "int".

Then, invert the last bit of its binary representation.


"last bit" is ambiguous. Does it refer to the most significant bit, or the
least significant bit, or the most significant bit which is set?

If it refers to the most significant bit, then does the sign bit count?

That is, if the last bit is 1, then change it to 0, and vice versa.

Duh.


The result should then be displayer as a decimal number. How does
the adjustment affect the resulting integer value?


If it's the least significant bit, then:

#include <iostream>
using std::cout;
using std::cin;

int main()
{
int i;

cin >> i;

cout << (i ^ 1);
}

If it's the most significant bit, and if the sign bit doesn't count, then:

#include <limits>
using std::numeric_limits;

#include <iostream>
using std::cout;
using std::cin;

int main()
{
int i;

cin >> i;

cout << (i ^ 1 << numeric_limits<int>::digits - 1);
}


As you can see there's a few ways of doing this.

If I wanted to be a right smart-ass, I would give all conceivable correct
answers.
 
H

Howard

Frederick Gotham said:
Hameed U. Khan posted:
If I wanted to be a right smart-ass, I would give all conceivable correct
answers.

Considering that there's an infinite number of ways to get any particular
mathematical result, that list would be mighty long... :)

-H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top