bitwise and operator
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: bitwise and operator
Multiples of 4 (0100) would be false, because their last two bits are always 0, and after and'ing with 3, the bits above bit 2 would be 0s as well
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: bitwise and operator
i & 1 means take i and bitwise And it with 1. Since this is an if statement essentially. If the result of the bitwise And is 1 then it will set offset.x equal to step. otherwise it will set it equal to -step.
The truth table for binary is below.
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
The truth table for binary is below.
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
CraigSpaz
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: bitwise and operator
Nope.Craigspaz wrote:i & 1 means take i and bitwise And it with 1. Since this is an if statement essentially. If the result of the bitwise And is 1 then it will set offset.x equal to step. otherwise it will set it equal to -step.
The truth table for binary is below.
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
Code: Select all
((i & 1) ? step : -step);
See other topic with same name. i & 3 will always be a number between 0 and 3, not 0 or 1 ( true or false ).
You may be just trying to oversimplify things, but this information is a 'bit' misleading. Bitwise operations are != to logical operations. The correct way would be:
if( ( i & 1 ) == 1 ) or
if( ( i & 4 ) == 4 ) or
if( ( ( i & 4 ) >> 3 ) == 1 )
The reason I bring this up is what if you do:
if( 12 & 4 )
12 != 4, but ( 1100 & 0100 ) returns 4 and since it's not 0, the CPU will gladly take the true path.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
- cyboryxmen
- Posts: 190
- Joined: November 14th, 2014, 2:03 am
Re: bitwise and operator
Remember that if statements are true for any non zero value. If one or more bits were 1s, they would all return true
Zekilk
Re: bitwise and operator
oh that makes sense. so the patterns for i & 3 would becyboryxmen wrote:Remember that if statements are true for any non zero value. If one or more bits were 1s, they would all return true
false
true
true
true
and repeat
and for even numbers it's basically
i & 2 = two each
i & 4 = 4 each
and so on
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: bitwise and operator
I wouldn't say for even numbers it's the number
0110 = 6
0000 & 0110 = 0
0001 & 0110 = 0
0010 & 0110 = 2
0011 & 0110 = 2
0100 & 0110 = 4
0101 & 0110 = 4
0110 & 0110 = 6
0111 & 0110 = 6
1000 & 0110 = 0
1010 = 10
0000 & 1010 = 0 -------- 0 & 10
0001 & 1010 = 0 -------- 1 & 10
0010 & 1010 = 2 -------- 2 & 10
0011 & 1010 = 2 -------- 3 & 10
0100 & 1010 = 0 -------- 4 & 10
0101 & 1010 = 0 -------- 5 & 10
0110 & 1010 = 2 -------- 6 & 10
0111 & 1010 = 2 -------- 7 & 10
1000 & 1010 = 8 -------- 8 & 10
1001 & 1010 = 8 -------- 9 & 10
1010 & 1010 = 10 ------ 10 & 10
1011 & 1010 = 10 ------ 11 & 10
1100 & 1010 = 8 ------- 12 & 10
1101 & 1010 = 8 ------- 13 & 10
1110 & 1010 = 10 ------ 14 & 10
1111 & 1010 = 10 ------ 15 & 10
Then the pattern repeats: 0,0,2,2,0,0,2,2,8,8,10,10,8,8,10,10
So it would be more correct to say powers of 2:
1,2,4,8,16,32,64,128
0000'0001 & any number -> {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,...}evens are 0, odds are 1
0000'0010 & any number -> {0,0,2,2,0,0,2,2,0,0,2,2,0,0,2,2,...} 0 to 1 = 0, 2 to 3 = 2
0000'0100 & any number -> {0,0,0,0,4,4,4,4,0,0,0,0,4,4,4,4,...} 0 to 3 = 0, 4 to 7 = 4
0000'1000 & any number -> {0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8...} 0 to 7 = 0, 8 to 15 = 8
0001'0000 & any number -> 0 to 15 = 0, 16 to 31 = 16, 32 to 48 = 0,...
0110 = 6
0000 & 0110 = 0
0001 & 0110 = 0
0010 & 0110 = 2
0011 & 0110 = 2
0100 & 0110 = 4
0101 & 0110 = 4
0110 & 0110 = 6
0111 & 0110 = 6
1000 & 0110 = 0
1010 = 10
0000 & 1010 = 0 -------- 0 & 10
0001 & 1010 = 0 -------- 1 & 10
0010 & 1010 = 2 -------- 2 & 10
0011 & 1010 = 2 -------- 3 & 10
0100 & 1010 = 0 -------- 4 & 10
0101 & 1010 = 0 -------- 5 & 10
0110 & 1010 = 2 -------- 6 & 10
0111 & 1010 = 2 -------- 7 & 10
1000 & 1010 = 8 -------- 8 & 10
1001 & 1010 = 8 -------- 9 & 10
1010 & 1010 = 10 ------ 10 & 10
1011 & 1010 = 10 ------ 11 & 10
1100 & 1010 = 8 ------- 12 & 10
1101 & 1010 = 8 ------- 13 & 10
1110 & 1010 = 10 ------ 14 & 10
1111 & 1010 = 10 ------ 15 & 10
Then the pattern repeats: 0,0,2,2,0,0,2,2,8,8,10,10,8,8,10,10
So it would be more correct to say powers of 2:
1,2,4,8,16,32,64,128
0000'0001 & any number -> {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,...}evens are 0, odds are 1
0000'0010 & any number -> {0,0,2,2,0,0,2,2,0,0,2,2,0,0,2,2,...} 0 to 1 = 0, 2 to 3 = 2
0000'0100 & any number -> {0,0,0,0,4,4,4,4,0,0,0,0,4,4,4,4,...} 0 to 3 = 0, 4 to 7 = 4
0000'1000 & any number -> {0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8...} 0 to 7 = 0, 8 to 15 = 8
0001'0000 & any number -> 0 to 15 = 0, 16 to 31 = 16, 32 to 48 = 0,...
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: bitwise and operator
now try doing it using hex ^.^
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat --
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat --
Re: bitwise and operator
albinopapa wrote:I wouldn't say for even numbers it's the number
0110 = 6
0000 & 0110 = 0
0001 & 0110 = 0
0010 & 0110 = 2
0011 & 0110 = 2
0100 & 0110 = 4
0101 & 0110 = 4
0110 & 0110 = 6
0111 & 0110 = 6
1000 & 0110 = 0
1010 = 10
0000 & 1010 = 0 -------- 0 & 10
0001 & 1010 = 0 -------- 1 & 10
0010 & 1010 = 2 -------- 2 & 10
0011 & 1010 = 2 -------- 3 & 10
0100 & 1010 = 0 -------- 4 & 10
0101 & 1010 = 0 -------- 5 & 10
0110 & 1010 = 2 -------- 6 & 10
0111 & 1010 = 2 -------- 7 & 10
1000 & 1010 = 8 -------- 8 & 10
1001 & 1010 = 8 -------- 9 & 10
1010 & 1010 = 10 ------ 10 & 10
1011 & 1010 = 10 ------ 11 & 10
1100 & 1010 = 8 ------- 12 & 10
1101 & 1010 = 8 ------- 13 & 10
1110 & 1010 = 10 ------ 14 & 10
1111 & 1010 = 10 ------ 15 & 10
Then the pattern repeats: 0,0,2,2,0,0,2,2,8,8,10,10,8,8,10,10
So it would be more correct to say powers of 2:
1,2,4,8,16,32,64,128
0000'0001 & any number -> {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,...}evens are 0, odds are 1
0000'0010 & any number -> {0,0,2,2,0,0,2,2,0,0,2,2,0,0,2,2,...} 0 to 1 = 0, 2 to 3 = 2
0000'0100 & any number -> {0,0,0,0,4,4,4,4,0,0,0,0,4,4,4,4,...} 0 to 3 = 0, 4 to 7 = 4
0000'1000 & any number -> {0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8...} 0 to 7 = 0, 8 to 15 = 8
0001'0000 & any number -> 0 to 15 = 0, 16 to 31 = 16, 32 to 48 = 0,...
power of 2s was actually what I thought in my head, not sure why I wrote even numbers
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: bitwise and operator
@Luis
I wouldn't say for even numbers it's the number
0x06 = 6
0x00 & 0x06 = 0
0x01 & 0x06 = 0
0x02 & 0x06 = 2
0x03 & 0x06 = 2
0x04 & 0x06 = 4
0x05 & 0x06 = 4
0x06 & 0x06 = 6
0x07 & 0x06 = 6
0x08 & 0x06 = 0
0x0A = 10
0x00 & 0x0A = 0 -------- 0 & 10
0x01 & 0x0A = 0 -------- 1 & 10
0x02 & 0x0A = 2 -------- 2 & 10
0x03 & 0x0A = 2 -------- 3 & 10
0x04 & 0x0A = 0 -------- 4 & 10
0x05 & 0x0A = 0 -------- 5 & 10
0x06 & 0x0A = 2 -------- 6 & 10
0x07 & 0x0A = 2 -------- 7 & 10
0x08 & 0x0A = 8 -------- 8 & 10
0x09 & 0x0A = 8 -------- 9 & 10
0x0A & 0x0A = 10 ------ 10 & 10
0x0B & 0x0A = 10 ------ 11 & 10
0x0C & 0x0A = 8 ------- 12 & 10
0x0D & 0x0A = 8 ------- 13 & 10
0x0E & 0x0A = 10 ------ 14 & 10
0x0F & 0x0A = 10 ------ 15 & 10
Then the pattern repeats: 0,0,2,2,0,0,2,2,8,8,10,10,8,8,10,10
So it would be more correct to say powers of 2:
1,2,4,8,16,32,64,128
0x01 & any number -> {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,...}evens are 0, odds are 1
0x02 & any number -> {0,0,2,2,0,0,2,2,0,0,2,2,0,0,2,2,...} 0 to 1 = 0, 2 to 3 = 2
0x04 & any number -> {0,0,0,0,4,4,4,4,0,0,0,0,4,4,4,4,...} 0 to 3 = 0, 4 to 7 = 4
0x08 & any number -> {0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8...} 0 to 7 = 0, 8 to 15 = 8
0x10 & any number -> 0 to 15 = 0, 16 to 31 = 16, 32 to 48 = 0,...
Definitely not as intuitive as binary for demonstrating bit manipulation and masking.
I wouldn't say for even numbers it's the number
0x06 = 6
0x00 & 0x06 = 0
0x01 & 0x06 = 0
0x02 & 0x06 = 2
0x03 & 0x06 = 2
0x04 & 0x06 = 4
0x05 & 0x06 = 4
0x06 & 0x06 = 6
0x07 & 0x06 = 6
0x08 & 0x06 = 0
0x0A = 10
0x00 & 0x0A = 0 -------- 0 & 10
0x01 & 0x0A = 0 -------- 1 & 10
0x02 & 0x0A = 2 -------- 2 & 10
0x03 & 0x0A = 2 -------- 3 & 10
0x04 & 0x0A = 0 -------- 4 & 10
0x05 & 0x0A = 0 -------- 5 & 10
0x06 & 0x0A = 2 -------- 6 & 10
0x07 & 0x0A = 2 -------- 7 & 10
0x08 & 0x0A = 8 -------- 8 & 10
0x09 & 0x0A = 8 -------- 9 & 10
0x0A & 0x0A = 10 ------ 10 & 10
0x0B & 0x0A = 10 ------ 11 & 10
0x0C & 0x0A = 8 ------- 12 & 10
0x0D & 0x0A = 8 ------- 13 & 10
0x0E & 0x0A = 10 ------ 14 & 10
0x0F & 0x0A = 10 ------ 15 & 10
Then the pattern repeats: 0,0,2,2,0,0,2,2,8,8,10,10,8,8,10,10
So it would be more correct to say powers of 2:
1,2,4,8,16,32,64,128
0x01 & any number -> {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,...}evens are 0, odds are 1
0x02 & any number -> {0,0,2,2,0,0,2,2,0,0,2,2,0,0,2,2,...} 0 to 1 = 0, 2 to 3 = 2
0x04 & any number -> {0,0,0,0,4,4,4,4,0,0,0,0,4,4,4,4,...} 0 to 3 = 0, 4 to 7 = 4
0x08 & any number -> {0,0,0,0,0,0,0,0,8,8,8,8,8,8,8,8...} 0 to 7 = 0, 8 to 15 = 8
0x10 & any number -> 0 to 15 = 0, 16 to 31 = 16, 32 to 48 = 0,...
Definitely not as intuitive as binary for demonstrating bit manipulation and masking.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: bitwise and operator
hehe
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat --
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat --