Toggle, clear and set bits

closed account (SGw5y60M)
I

a) Toggles bit 11 in x.

b) Clears all bits except bit 3 in x.

c) Sets bits 0,1,2,3 in x.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  a) // toggles bit 11 in x

x ^= 1 <<11;

//*********************************

b) // clearall bits except bit 3 (fourth bit) in x

x&=(1<<3);

std::cout << (int) x << std::endl;

//*************************************

c) //set bits 0,1,2,3

for (int i = 0; i !=4; i++)

x |= (1<<i);

std::cout << (int)x << std::endl;

std::cout << '\n';


[/code]
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <iomanip>

int main()
{
	unsigned x {};

	// toggles bit 11 (twelfth bit) in x
	x ^= 1 << 11;

	std::cout << std::hex << x << '\n';

	//*********************************

	// clear all bits except bit 3 (fourth bit) in x

	x = 0xff;

	x &= (1 << 3);

	std::cout << std::hex << x << '\n';

	//*************************************

	//set bits 0,1,2,3

	x |= 0xf;

	std::cout << std::hex << x << '\n';
}



800
8
f

Last edited on
closed account (SGw5y60M)
can you please explain why you put hex and not put int ??

in this line std::cout << std::hex << x << '\n';
std::hex is an I/O manipulator that reads/writes integer values as hexadecimal numbers.
http://www.cplusplus.com/reference/ios/hex/

It is NOT needed, but having hex numbers on output makes it easier to interpret the "bitness." the variable is still an int.

(int) is a C style cast, telling the compiler the variable is to be interpreted as an integer no matter what its actual type is.
Last edited on
I believe seeplus has a typo. x |= 0xff; sets bits 0-7. You want x |= 0xf;
Thanks. Yes - should be 0xf. Definitely a bounce on the keyboard f key. Didn't check the result as the code was correct of course!

I know I need a new keyboard. I'll have to get one. Anyone got a favourite - other than a Microsoft 600.

Fixed above.
Last edited on
seeplus wrote:
I know I need a new keyboard. I'll have to get one. Anyone got a favourite - other than a Microsoft 600.
Great question. I'm interested in what others have to say also. I'll start a thread in the lounge.
Last edited on
closed account (SGw5y60M)
thanks again,
I noticed that you guys talking about error in the code above, could you please rewrite the code above in a new correct way and thanks for your effort helping me

the important here is not to write a program but to solve the a b c as syntax
Last edited on
The code in my post above is now correct - as I mentioned in my previous post. It was a simple typo caused by a faulty keyboard.
My favorite is my Apple Magic Keyboard. I have used several other types, including Apple's latest version on their newest MacBook Pro (13"), and this one is still my favorite.

It has lasted me 12 years without a glitch or a grumble, and the keys aren't wearing off! Unlike my PC keyboard at work, which I replaced 4 times over about 5 years due to letters wearing off or "going bad," a term which covers a number of electronic issues. Finally I got a Magic Keyboard to hook up to the PC and that solved it.

Edit:
Check out these two threads:
https://www.cplusplus.com/forum/lounge/277853/
https://www.cplusplus.com/forum/lounge/276240/
Last edited on
Topic archived. No new replies allowed.