Bitwise & and I - c

Why is 1 & 4 = 0
whereas
1 | 4 evaluates to 5

Well.. because.
For &, the AND operator:
0001 = 1
0100 = 4
---- (AND)
0000 = 0
for |, the OR operator:
0001 = 1
0100 = 4
---- (OR)
0101 = 5

Bitwise & => If both bits are higher, then the output is higher else output is zero.
0 0 1
1 0 0
-----
0 0 0 => 0 // 1 & 1 = 1 , 1 & 0 = 0
Now try yourself Bitwise |. Any of the bit is higher, output is higher.

1 is 0b001, and 4 is 0b100, so, naturally, 1&4 is 0b000 and 1|4 is 0b101, which is 5.

Look at it in binary form.
1d(ecimal) = 001b(inary)
4d(ecimal) = 100b(inary)
thus
001b
100b & (both bits have to be 1 to yield 1)
--
000b = 0d
and
001b
100b | (only one on either side (or both) has to be 1 to yield 1)
--
101b = 5d

Related

Using Bitwise Operators in C [duplicate]

This question already has answers here:
How does this work? Weird Towers of Hanoi Solution
(3 answers)
Closed 3 years ago.
I am a beginner to C language.I have a code for towers of hanoi but can someone explain me what are these bitwise operators doing ie if value of i is 1 what will be the source and target output value ?
source = (i & i-1) % 3;
target = ((i | i-1) + 1) % 3;
i & i-1 turns off the lowest set bit in i (if there are any set). For example, consider i=200:
200 in binary is 1100 1000. (The space is inserted for visual convenience.)
To subtract one, the zeros cause us to “borrow” from the next position until we reach a one, producing 1100 0111. Note that, working from the right, all the zeros became ones, and the first one became a zero.
The & produces the bits that are set in both operands. Since i-1 changed all the bits up to the first one, those bits are clear in the &—none of the changed bits are the same in both i and i-1, so none of them is a one in both. The other ones in i, above the lowest one bit, are the same in both i and i-1, so they remain ones in i & i-1. The result of i & i-1 is 1100 0000.
1100 0000 is 1100 1000 with the lowest set bit turned off.
Then the % 3 is selecting which pole in Towers of Hanoi to use as the source. This is discussed in this question.
Similarly i | i-1 turns on all the low zeros in i, all the zeros up to the lowest one bit. Then (i | i-1) + 1 adds one to that. The result is the same as adding one to the lowest one bit in i. That is, the result is i + x, where x is the lowest bit set in i. Using our example value:
i is 1100 1000 and i-1 is 1100 0111.
i | i-1 is 1100 1111.
(i | i-1) + 1 is 1101 0000, which equals 1100 1000 + 0000 1000.
And again, the % 3 selects a pole.
A quick overview of bitwise operators:
Each operator takes the bits of both numbers and applies the operation to each bit of it.
& Bitwise AND
True only if both bits are true.
Truth table:
A | B | A & B
-------------
0 | 0 | 0
1 | 0 | 0
0 | 1 | 0
1 | 1 | 1
| Bitwise OR
True if either bit is true.
Truth table:
A | B | A | B
-------------
0 | 0 | 0
1 | 0 | 1
0 | 1 | 1
1 | 1 | 1
^ Bitwise XOR
True if only one bit is true.
Truth table:
A | B | A ^ B
-------------
0 | 0 | 0
1 | 0 | 1
0 | 1 | 1
1 | 1 | 0
~ Bitwise NOT
Inverts each bit. 1 -> 0, 0 -> 1. This is a unary operator.
Truth table:
A | ~A
------
0 | 1
1 | 0
In your case, if i = 1,
the expressions would be evaluated as:
source = (1 & 1-1) % 3;
target = ((1 | 1-1) + 1) % 3;
// =>
source = (1 & 0) % 3;
target = ((1 | 0) + 1) % 3;
// =>
source = 0 % 3;
target = (1 + 1) % 3;
// =>
source = 0;
target = 2 % 3;
// =>
source = 0;
target = 2;
Good answer above, here is a high-level approach:
i == 1:
source: (1 & 0). Are both of these values true or >= 1? No they are not. So the overall result is 0, 0 % 3 = 0.
target: ((1 | 0) + 1) % 3.
(1 | 0) evaluates to 1(true) since one of the two values on the sides of the | operator are 1, so now we have (1 + 1). so then it follows we have 2 % 3 = 2.
Source: 0, target: 2

swap hex binary with rule

please i need a how to do in linux for swap job.
my exemple :
9882 swaped to 016C
and
3030 swaped to 0303
like this
1001 1000 1000 0010 = 9882 : bin
0000 0001 0110 1100 = 016C : bin swap
bin before swap :
0 1 2 3 4 5 6 7 8 9 A B C D E F
1 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0
bin after swap :
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 0 0 0 0 0 1 0 1 1 0 1 1 0 0
with swap rule :
bin_SWAP[0] = bin[D];
bin_SWAP[1] = bin[5];
bin_SWAP[2] = bin[6];
bin_SWAP[3] = bin[7];
bin_SWAP[4] = bin[9];
bin_SWAP[5] = bin[1];
bin_SWAP[6] = bin[2];
bin_SWAP[7] = bin[3];
bin_SWAP[8] = bin[C];
bin_SWAP[9] = bin[4];
bin_SWAP[A] = bin[E];
bin_SWAP[B] = bin[F];
bin_SWAP[C] = bin[8];
bin_SWAP[D] = bin[0];
bin_SWAP[E] = bin[A];
bin_SWAP[F] = bin[B];
there is any program in linux can swap a file with more than 64 bytes
my exmple do with 2 bytes.
thanks in advance.
another exmple for more simplify
swap 4 bytes
B455CBD5
B455
1011 0100 0101 0101 : bin
0123 4567 89AB CDEF : position
1100 1011 0001 0101 : bin swap
CB15
CBD5
1100 1011 1101 0101 : bin
0123 4567 89AB CDEF : position
1011 1100 0111 1101 : bin swap
BC7D
than the result is
CB15BC7D

Why can 4 bits store values in the range 0-15?

struct bitCard {
unsigned int face : 4;
unsigned int suit : 2;
unsigned int color : 1;
};
"The preceding structure definition indicates that member face is stored in 4 bits,
member suit is stored in 2 bits and member color is stored in 1 bit. The number of bits is based on the desired range of values for each structure member. Member face stores
values from 0(Ace) through 12(King)—4 bits can store values in the range 0–15." (C how to program).
The sentence in bold makes me confused since I cannot help myself understand why 4 bits can store values from 0 to 15. Can anyone help me out?
think 2 in the power of 4 = 16 possible values...
binary value represent decimal number in range 0..2^x - where X=num of bits
Because 1+2+4+8 = 15 it follows that 4-digit binary numbers can range from 0 to 15. The numbers are:
0000b = 0d
0001b = 1d
0010b = 2d
0011b = 3d
...
1110b = 14d
1111b = 15d
as there only 16 unique combinations (thanks Udo) of 0,1 that you can represent with 4 bits
0000 0
0001 1
0010 2
0011 3
.........
1111 15
because 15 = 1 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1*2^0 ie: 15 = 0xF = 1111b
Where ^ is the power operation.
Or, more detailed:
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = 10
1011 = 11
1100 = 12
1101 = 13
1110 = 14
1111 = 15
I really can recommend reading the wikipedia article about binary numbers: http://en.wikipedia.org/wiki/Binary_number

C Programming - XOR Bitwise Operation

What operation does the following ‘C’ statement perform?
star = star ^ 0b00100100;
(A) Toggles bits 2 and 5 of the variable star.
(B) Clears all bits except bits 2 and 5 of the variable star.
(C) Sets all bits except bits 2 and 5 of the variable star.
(D) Multiplies value in the variable star with 0b00100100.
I'm still clueless about this. Can someone help me out?
XOR operator (also called "logical addition") is defined like this:
a b a^b
-----------
0 0 0
0 1 1
1 0 1
1 1 0
So a^0 leaves a intact while a^1 toggles it.
For multiple-bit values, the operation is performed bitwise, i.e. between corresponding bits of the operands.
If you know how XOR works, and you know that ^ is XOR in C, then this should be pretty simple. You should know that XOR will flip bits where 1 is set, bits 2 and 5 of 0b00100100 are set, therefore it will flip those bits.
From an "during the test" standpoint, let's say you need to prove this to yourself, you really don't need to know the initial value of star to answer the question, If you know how ^ works then just throw anything in there:
00100100
^10101010 (star's made up value)
---------
10001110 (star's new value)
bit position: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
|---|---|---|---|---|---|---|---
star's new v: | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 0
|---|---|---|---|---|---|---|---
star's old v: | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0
Then check your answers again, did it:
(A) Toggles bits 2 and 5 of the variable star. (Yes)
(B) Clears all bits except bits 2 and 5 of the variable star. (Nope)
(C) Sets all bits except bits 2 and 5 of the variable star. (Nope)
(D) Multiplies value in the variable star with 0b00100100. (36x170 = 142? Nope)
It is (A) toggles bits 2 and 5.
The following is the truth table for the XOR operation:
x y x^y
0 0 0
1 0 1
0 1 1
1 1 0
You can see from the table that x XOR 0 = x and x XOR 1 = !x.
XOR is a bitwise operation, so it operates on individual bits. Therefore if you XOR star with some constant, it will toggle the 1 bits in the constant.
You can find some explanation e.g. here.
The exclusive OR has this truth table:
A B A^B
-----------
1 1 0
1 0 1
0 1 1
0 0 0
We can see that if B is true (1) then A is flipped (toggled), and if it's false (0) A is left alone. So the answer is (A).
XOR operator returns 0 if both inputs are same otherwise returns 1 if both inputs are different.For Example the Given Truth Table :-
a=1 b=1 => a^b=0,
a=0 b=0 => a^b=0,
a=0 b=1 => a^b=1,
a=1 b=0 => a^b=1.
well xor is binary operator that work on bits of 2 nos.
rule of xoring:for same bit ans is 0 and for different bit ans is 1
let
a= 1 0 1 0 1 1
b= 0 1 1 0 1 0
--------------
c= 1 1 0 0 0 1
--------------
compare bit of a and b bit by bit
if same put 0 else put 1
xor is basically used to find the unique in given set of duplicate no.
just xor all nos. and u will get the unique one(if only single unique is present)

Changing the LSB to 1 in a 4 bit integer via C

I am receiving a number N where N is a 4-bit integer and I need to change its LSB to 1 without changing the other 3 bits in the number using C.
Basically, all must read XXX1.
So lets say n = 2, the binary would be 0010. I would change the LSB to 1 making the number 0011.
I am struggling with finding a combination of operations that will do this. I am working with: !, ~, &, |, ^, <<, >>, +, -, =.
This has really been driving me crazy and I have been playing around with >>/<< and ~ and starting out with 0xF.
Try
number |= 1;
This should set the LSB to 1 regardless of what the number is. Why? Because the bitwise OR (|) operator does exactly what its name suggests: it logical ORs the two numbers' bits. So if you have, say, 1010b and 1b (10 and 1 in decimal), then the operator does this:
1 0 1 0
OR 0 0 0 1
= 1 0 1 1
And that's exactly what you want.
For your information, the
number |= 1;
statement is equivalent to
number = number | 1;
Use x = x | 0x01; to set the LSB to 1
A visualization
? ? ? ? ? ? ? ?
OR
0 0 0 0 0 0 0 1
----------------------
? ? ? ? ? ? ? 1
Therefore other bits will stay the same except the LSB is set to 1.
Use the bitwise or operator |. It looks at two numbers bit by bit, and returns the number generated by performing an OR with each bit.
int n = 2;
n = n | 1;
printf("%d\n", n); // prints the number 3
In binary, 2 = 0010, 3 = 0011, and 1 = 0001
0010
OR 0001
-------
0011
If n is not 0
n | !!n
works.
If n is 0, then !n is what you want.
UPDATE
The fancy one liner :P
n = n ? n | !!n : !n;

Resources