Question:
what does "nth bit of a number is set" mean?
2017-05-04 21:22:49 UTC
I was assigned to do this:

unsigned int SetNthBit(unsigned int Number, int n) ;
//sets the nth bit of Number and returns the result

and

bool NthBitIsSet(unsigned int Number, int n) ;
//returns true if the nth bit of Number is set

I don't understand what it means to check nth bit of a number is set... help and thanks in advance!
Three answers:
2017-05-08 09:52:17 UTC
The binary representation of a number consists of a sequence of bits ('binary digits") each of which may have the value 0 or 1.



The bits are numbered starting from 0, with bit 0 being the least significant bit.



Describing a bit as being "set" or "clear" is just another way of saying that the bit has the value 1 (set) or 0 (clear).



To find if the Nth bit of an integer is set you can use a shift operation to check the value of only that one specific bit.



For example:



bool NthBitIsSet(unsigned int Number, int n)



return (Number and (1 << n)) != 0;
husoski
2017-05-04 23:48:17 UTC
Bit manipulation is basically inspecting or changing one or more bits in the binary representation of a number and leaving the other bits alone.



You didn't mention your bit numbering order, so I assume that it's the so-called "little endian" version that starts with the least significant bit as "bit 0". If not, then you'll need to alter what follows accordingly.



In languages based on C, the easiest way to inspect and modify single bits is by using the "bitwise" operators:

.... a & b : each bit of the result is the AND of the same bits of arguments a and b

.... a | b : each bit of the result is the OR of the corresponding bits of a and b.

.... a ^ b : each bit of the result is the XOR (exclusive or) of the corresponding bits of a and b

.... ~a : each bit of the result is the opposite (complement) of the corresponding bit of a

.... a << k : returns a value with all bits shifted left by k bits

.... a >> k : returns a value with all bits shifted right by k bits



Those are the basics. The shifts always fill zeros on the right when shifted left. Right shifts of unsigned data also fill zeroes on the left, but right shifts of signed values fill with copies of the original sign bit.



With those operations (good in C#, Python, Java, as well as in the original C/C++) you can use:



1<
a&(1<
a|(1<
a&~(1<
a^(1<


For handling multiple contiguous bits as a single value, the following are handy to remember:



((1<
a & ((1<
a & ~((1<


It used to be a common thing to use function-like #define macros to make pseudo-functions like setbit(a,k), cleearbit(a,k), etc. to perform these operations without actual function calls.



If you want to learn this stuff, what I suggest you do is play around with those operations with pencil and paper on small (8-bit maybe) values and watch what happens with each operation. That should help you "get" what's going on and maybe come up with new patterns on your own as the need arises.
John
2017-05-04 21:50:04 UTC
To check if the zeroth bit was set would mean to check to see if the

number was odd or not. If the zeroth bit were set, the number would

look like baba bba1, where the b's and a's are binary numbers. 'Set' just

means 'one', and 'cleared' means 'zero'.

>

> John (gnujohn)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...