I'm writing a program that exchanges the values of the bits on positions 3, 4 and 5 with bits on positions 24, 25 and 26 of a given 32-bit unsigned integer.
So lets say I use the number 15 and I want to turn the 4th bit into a 0, I'd use...
int number = 15
int newnumber = number & (~(1 << 3));
// output is 7
This makes sense because I'm exchanging the 4th bit from 1 to 0 so 15(1111) becomes 7(0111).
However this wont work the other way round (change a 0 to a 1), Now I know how to achieve exchanging a 0 to a 1 via a different method, but I really want to understand the code in this method.
So why wont it work?
The truth table for x AND y is:
x y Output
-----------
0 0 0
0 1 0
1 0 0
1 1 1
In other words, the output/result will only be 1 if both inputs are 1, which means that you cannot change a bit from 0 to 1 through a bitwise AND. Use a bitwise OR for that (e.g. int newnumber = number | (1 << 3);)
To summarize:
Use & ~(1 << n) to clear bit n.
Use | (1 << n) to set bit n.
To set the fourth bit to 0, you AND it with ~(1 << 3) which is the negation of 1000, or 0111.
By the same reasoning, you can set it to 1 by ORing with 1000.
To toggle it, XOR with 1000.
Related
This statement checks whether a number is 32 bits.
0 < res <= (1 << 31) -1
I can't seem to understand how, can someone help understand this bit shift syntax?
Well, lets begin with an example:
1 in binary is 1
2 in binary is 10
4 in binary is 100
We can see that we need to 'add' an 0 at the end of each number to multiply by 2 and in most language we can do this with this syntax: number << 1
Here we are saying that we add a 1 time a 0 to the left. number >> 1 and here we add 1 time a 0 to the right.
So 1 << 31 means 1 * 2 * 2 * 2 ... 31 times which means 2^31 (so 32 bits)
I have a 2 bit number x stored in a single byte, and I would like to invert it, so that 0 is transformed to 3, 1 to 2, 2 to 1 and 3 to 0.
Isx^3be the correct way of doing this?
Also, for arbitrary N bit number x, will x^((1<<N)-1) be correct ?
x^3 is good for two bit numbers. If you have 4 bit numbers, use x ^ 15, or write it in hex, x ^ 0x0f if you want.
No, x^(N-1) will not work. What will work is x^(pow(2,N)-1)
Code :
((~x)&(0x3))
Test Code (Same piece of code should work in C++ too):
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Hello");
for(int i=0; i<=3; i++) {
System.out.println("Complement of " + i + " is " + (~i & 0x3));
}
}
Result:
Complement of 0 is 3
Complement of 1 is 2
Complement of 2 is 1
Complement of 3 is 0
I am doing a project on digital filters. I needed to know how to add a 4 bit binary number to the most significant 4 bits of an 8 bit number. For example:
0 1 0 0 0 0 0 0 //x
+ 1 0 1 0 //y
= 1 1 1 0 0 0 0 0 //z
Can I add using a code somewhat like this?
z=[7:4]x + y
or should I have to concatenate the 4 bit number with another four zeros and add?
Assuming y is the 4 bit number and x the 8 bit number:
If you do
assign z = x[7:4] + y
Then you are doing a 4-bit addition and the most significant part of z is padded with 0's.
If you do
assign z = y[7:4] + x
You will get an error message from the synthesizer, as subscripts for y are wrong.
So do as this:
assign z = {y,4'b0} + x
Which performs an 8-bit addition with x and the value of y shifted 4 bits to the left, which is want you wanted.
I want to use parity check function in a loop to make an event that happens every second times. But all the functions I found to watch parity don't work.
Even the most simple ones, like this:
unsigned int v;
bool parity = false;
while (v)
{
parity = !parity;
v = v & (v - 1);
}
Can give me the same value for an even number and for an odd number.
How to do it well? Or maybe, are there any other ways to to make an event that happens in every second round through the loop?
It seems like you're misunderstanding the meaning of "parity" as it's normally used in the world of computers. The code you've shown sets parity to false if the binary representation of v has an even number of set bits, and to true if it has an odd number of set bits. The concept of parity has nothing to do with v being even or odd itself. Some examples:
Number (base 10) Number (base 2) Parity
0 0 0
1 1 1
2 10 1
3 11 0
4 100 1
5 101 0
6 110 0
7 111 1
. . .
. . .
. . .
12498741 1101 1110 1101 0111 0011 0101 0
. . .
. . .
. . .
If you really do want to check if a number is even or odd, you can use:
bool odd = v & 1;
bool even = !(v & 1);
Which are equivalent to the % expression used in #user1118321's answer.
Try this:
bool isEven = ((v % 2) == 0);
This assumes v is the variable you wish to test for evenness. This is modulo arithmetic.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
How do you set, clear and toggle a single bit in C?
Removing lowest order bit
n is a positive integer. How can its rightmost set bit be unset?
Say n= 7 => n = 0111.
I want 0110 as the output. Is there any simple bitwise hack to achieve the goal?
Try n & (n-1) where & is bitwise AND
n = 7
n - 1 =6
n & (n-1)=> 0 1 1 1 (7)
& 0 1 1 0 (6)
---------
0 1 1 0 (done!)
EDIT (in response to the comment given by Forest)
n = 6
n - 1 = 5
n & (n-1)=> 0 1 1 0 (6)
& 0 1 0 1 (5)
---------
0 1 0 0 (done!)
Your question is unclear.
If you just want to unset bit 0, here are some methods (with slight variations in behavior depending on your types involved):
x &= -2;
x &= ~1;
x -= (x&1);
If you want to unset the lowest bit among the bits that are set, here are some ways:
x &= x-1;
x -= (x&-x);
Note that x&-x is equal to the lowest bit of x, at least when x is unsigned or twos complement. If you want to do any bit arithmetic like this, you should use only unsigned types, since signed types have implementation-defined behavior under bitwise operations.
unsigned int clr_rm_set_bit(unsigned int n)
{
unsigned int mask = 1;
while(n & mask) {
mask <<= 1;
}
return n & ~mask;
}