Jump to content

C - Bit Patterns Help

Go to solution Solved by mariushm,

If i'm reading it right, he probably means to set the n-th bit to 1 in the variable. 

An unsigned short is 16 bit wide ,  so you have bit 15 , bit 14 , bit 13  .... bit 1 , bit 0  so if you enter 3 , 13 , 8  that means you want the 4th bit from right set to 1 (because you start from bit 0), then you set the 14th bit from the right and so on.

 

You set and unset bits with the OR and XOR operators.. quoting quora.com article which quotes stackoverflow because i'm too lazy and don't have these memorized :

 

https://www.quora.com/How-do-you-set-clear-and-toggle-a-single-bit-in-C

 

Quote

Setting a bit

Use the bitwise OR operator (|) to set a bit.

    number |= 1 << x;

That will set bit x.

 

Clearing a bit

Use the bitwise AND operator (&) to clear a bit.

    number &= ~(1 << x);

That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it.

 

Toggling a bit

The XOR operator (^) can be used to toggle a bit.

    number ^= 1 << x;

That will toggle bit x.

 

So for each number , make sure it's between 0 and 15 inclusive,  then maybe clear that bit just in case it's already set, then set the bit to 1

 

If it's not obvious, the << shifts a variable to the left by the number of bits you specify ...

so "1"  is 00000000 00000001 in binary and if you say 1 <<3 it becomes  00000000 00001000 and if you say number = number OR variable then that bit is set to 1 because 0 or 1 = 1 , 0 or 0 = 0 , 1 or 1 = 0.

So my professor assigned a homework that states "your program should accept a list of integers in the range 0-15 and turn on the corresponding bits in an unsigned short to output a result that shows set membership. input 2 3 4 then your output will print 0000000000011100. I'm not entirely sure what turning on the corresponding bits in an unsigned short to output a result that shows set membership means. Could someone just rephrase this for me, I'm having trouble figuring out what this means.

He also gives this example but I have no idea what operations are done to get that binary number

3 13 8 14 15 
1110 0001 0000 1000 

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/860488-c-bit-patterns-help/
Share on other sites

Link to post
Share on other sites

If i'm reading it right, he probably means to set the n-th bit to 1 in the variable. 

An unsigned short is 16 bit wide ,  so you have bit 15 , bit 14 , bit 13  .... bit 1 , bit 0  so if you enter 3 , 13 , 8  that means you want the 4th bit from right set to 1 (because you start from bit 0), then you set the 14th bit from the right and so on.

 

You set and unset bits with the OR and XOR operators.. quoting quora.com article which quotes stackoverflow because i'm too lazy and don't have these memorized :

 

https://www.quora.com/How-do-you-set-clear-and-toggle-a-single-bit-in-C

 

Quote

Setting a bit

Use the bitwise OR operator (|) to set a bit.

    number |= 1 << x;

That will set bit x.

 

Clearing a bit

Use the bitwise AND operator (&) to clear a bit.

    number &= ~(1 << x);

That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it.

 

Toggling a bit

The XOR operator (^) can be used to toggle a bit.

    number ^= 1 << x;

That will toggle bit x.

 

So for each number , make sure it's between 0 and 15 inclusive,  then maybe clear that bit just in case it's already set, then set the bit to 1

 

If it's not obvious, the << shifts a variable to the left by the number of bits you specify ...

so "1"  is 00000000 00000001 in binary and if you say 1 <<3 it becomes  00000000 00001000 and if you say number = number OR variable then that bit is set to 1 because 0 or 1 = 1 , 0 or 0 = 0 , 1 or 1 = 0.

Link to comment
https://linustechtips.com/topic/860488-c-bit-patterns-help/#findComment-10706092
Share on other sites

Link to post
Share on other sites

9 hours ago, mariushm said:

If i'm reading it right, he probably means to set the n-th bit to 1 in the variable. 

An unsigned short is 16 bit wide ,  so you have bit 15 , bit 14 , bit 13  .... bit 1 , bit 0  so if you enter 3 , 13 , 8  that means you want the 4th bit from right set to 1 (because you start from bit 0), then you set the 14th bit from the right and so on.

 

You set and unset bits with the OR and XOR operators.. quoting quora.com article which quotes stackoverflow because i'm too lazy and don't have these memorized :

 

https://www.quora.com/How-do-you-set-clear-and-toggle-a-single-bit-in-C

 

 

So for each number , make sure it's between 0 and 15 inclusive,  then maybe clear that bit just in case it's already set, then set the bit to 1

 

If it's not obvious, the << shifts a variable to the left by the number of bits you specify ...

so "1"  is 00000000 00000001 in binary and if you say 1 <<3 it becomes  00000000 00001000 and if you say number = number OR variable then that bit is set to 1 because 0 or 1 = 1 , 0 or 0 = 0 , 1 or 1 = 0.

Oh wow now I get it, thanks for the writeup!

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/860488-c-bit-patterns-help/#findComment-10706363
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...
On 11/16/2017 at 10:25 AM, mariushm said:

If i'm reading it right, he probably means to set the n-th bit to 1 in the variable. 

An unsigned short is 16 bit wide ,  so you have bit 15 , bit 14 , bit 13  .... bit 1 , bit 0  so if you enter 3 , 13 , 8  that means you want the 4th bit from right set to 1 (because you start from bit 0), then you set the 14th bit from the right and so on.

 

You set and unset bits with the OR and XOR operators.. quoting quora.com article which quotes stackoverflow because i'm too lazy and don't have these memorized :

 

https://www.quora.com/How-do-you-set-clear-and-toggle-a-single-bit-in-C

 

 

So for each number , make sure it's between 0 and 15 inclusive,  then maybe clear that bit just in case it's already set, then set the bit to 1

 

If it's not obvious, the << shifts a variable to the left by the number of bits you specify ...

so "1"  is 00000000 00000001 in binary and if you say 1 <<3 it becomes  00000000 00001000 and if you say number = number OR variable then that bit is set to 1 because 0 or 1 = 1 , 0 or 0 = 0 , 1 or 1 = 0.

Okay I thought I got it but I lied. How would I do this multiple times without the other shifts and operations messing it up

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/860488-c-bit-patterns-help/#findComment-10775690
Share on other sites

Link to post
Share on other sites

19 hours ago, CJPowell27 said:

Okay I thought I got it but I lied. How would I do this multiple times without the other shifts and operations messing it up

You or it with the variable that's storing this data, e.g.,

output = 0;
output |= (1 << 3);
output |= (1 << 8);
output |= (1 << 13);
output |= (1 << 14);
output |= (1 << 15);

 

Link to comment
https://linustechtips.com/topic/860488-c-bit-patterns-help/#findComment-10778575
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×