#include <stdio.h>
int main()
{
int decimal_num, c, result;
printf("Enter an integer in decimal number system\n");
scanf("%d", &decimal_num);
for (c = 31; c >= 0; c--)
{
result = decimal_num >> c;
if (result & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
This code takes a decimal number and converts it into binary using bitwise operator. I am having a hard time understanding the logic inside the for loop result = decimal_num >> c and why does it iterate from for (c = 31; c >= 0; c--). I understand the basics of bitwise AND, OR, XOR and NOT and I know that when an odd number is ANDED with '1' the result is '1' else '0'(because the least significant bit of all odds are 1).
Here's an explanation of the code :
The program scans the bitwise representation of the decimal digit from left to write, working on each bit. The decimal digit is supposed to have 32 bits, hence the for loop runs 32 times.
The first time, the value of c is 31.
Assuming the bit representation of decimal_num initially is
x................................ ( . represents any digit )
decimal_num >> 31 shifts all bits rightwards 31 times, such that the first bit is shifted at the right most end. The result is 0000000000000000000000000000x. Note that when digits are shifted, 0 is pre-pended to the left end.
The result is then checked to see if it was 0 or 1, and printed accordingly.
0000000000000000000000000000x & 00000000000000000000000000001 = 1 if x is one
0000000000000000000000000000x & 00000000000000000000000000001 = 0 if x is zero.
Moving on, and checking the second bit when c is 30. :
.Y..............................
decimal_num >> 30 results in
000000000000000000000000000000.Y
000000000000000000000000000.Y & 00000000000000000000000000001 = 1 if Y is one
000000000000000000000000000.Y & 00000000000000000000000000001 = 0 if Y is zero.
We go on printing the results until the last digit.
Hope this helps you understand.
Related
I'm trying to left rotate binary value:
int left_side=0;
for (int i = 0; i < n; i++)
{
left_side = left_side | ( ( number & ( 1<<BITS-i )) >> (BITS+i+1)-n );
}
BITS indicates the length of the binary value, n is the distance for rotation.
Example: 1000 , and n=1 which means the solution will be: 0001.
Some reason that I don't understand when I rotate it (from left to right side), lets take an example for number 253 which in binary sequence is 11111101 and n=3 (distance), the result from my code in binary sequence is 101 (which is 5).
Why the answer isn't 7? What I missed in my condition in this loop?
Thanks.
You want to rotate left your number n of a specific amount of bits.
Thus you have to shift your number to the left using n << amount and put the left bits to the right. This is done by putting the bits [0-amount[ to [NUMBER_BITS-amount,NUMBER_BITS[` using right shift.
For instance, if your number is an uint32_t you can use the following code (or easily adapt it to other types).
uint32_t RotateLeft(uint32_t n, int amount) {
return (n << amount)|(n >> (8*sizeof(uint32_t) - amount));
}
I think there are 2 approaches:
rotate by 1 bit n times
rotate by n % BITS bits only once
The rotation "wraps over" so we could just modulo every BITSth (8th) multiple of n to 0 in case someone would want to rotate 100 times. I think the second approach would be easier to understand, implement and read and why loop 100 times, if it can be done once?
Algorithm:
I will use 8 bits for demonstration, even though int is minimum 16 bits. The original MSB is marked by a dot.
.11110000 rl 2 == 110000.11
Well what has happened? 2 bits went right and the rest (BITS - 2) went left. That is just shift left and shift right "combined".
a = .11110000 << 2 == 110000.00
b = .11110000 >> (BITS - 2) == 000000.11
c = a | b
c == 110000.11
Easy, isn't it? Just remember to use n % BITS first and to use an unsigned type.
unsigned int rotateLeft(unsigned int number, int n) {
n %= BITS;
unsigned int left = number << n;
unsigned int right = number >> (BITS - n);
return left | right;
}
I saw the code and was wondering why this is working:
#include <stdio.h>
void printBinary (unsigned int number)
{
unsigned int mask = 0;
for(mask = 1 << 31; mask!=0; mask >>=1)
{
if (number & mask)
printf("1");
else
printf("0");
}
printf ("\n");
}
int main()
{
printBinary (10);
return 0;
unsigned int suffix = 24;
return 0;
}
I imagine the void function like this:
at first we have
mask: 100000000000000... (32times zero) and
number: 0000000000000000000001010
the it compares both numbers with & and they are different, so 0 is posted.
next loop:
01000000000....
0000000000000000000001010
same here. When it comes to
00000000000000001000
00000000000000001010
(number & mask) will be true, but still both numbers are differnt. It's like it is only comparing that the binary numbers where the 1 currently is in the upper number. But since mask and number as such is compared it should only print 1 if they are completely the same. So what I don't understand is why a 1 is printed here instead of a 0. I think a 1 should only be printed if it's
0000000000000001010
0000000000000001010
I hope it is clear what my question is, maybe someone can explain to me how this loop works in detail.
Thanks!
& is a bitwise AND operation, so
00000000000000001000 & 00000000000000001010
evaluates to
00000000000000001000
which is different from 0, and in C
if (number & mask)
is shorthand for
if ((number & mask) != 0)
The work is done by this algorithm:
for(mask = 1 << 31; mask!=0; mask >>=1) {
if (number & mask)
printf("1");
else
printf("0");
}
The first thing to understand is the "mask" - the mask here is a 32-bit integer with EXACTLY 1 bit set to 1 and all the others are zero. You seem to have understood that the mask starts as "1000000...000" and on each successive loop, the "1" in the mask is shifted to the right (mask >>= 1). So the sequence on successive loop iterations is: "01000....000", "001000...000", ... "0000... 00010", "0000...00001"
The next thing is the "&" operator - "bit-wise AND" - when 2 numbers are "bit-wise AND"ed together, the result will have 1 bits ONLY in positions where both the inputs had 1 bits. So, for example, "00010" & "11010" == "00010". Since your "mask" only has 1 bit set, the result of (number & mask) will be identical to the mask itself if the bit in that same positiion in number is a 1 and will be 0 if not.
Lastly, we note that in C, 0 is false and ANYTHNG else is considered true.
Thus, we can read the loop above as follows:
For each bit from position 31 to position 0:
if that bit is a 1 in the number:
print 1
else:
print 0
Voila - the number is printed in its binary form.
the bitwise AND (&) result is a number in which all the bits that were set (1) in the 2 operands, are set.
this means that if a single bit matches, just as you observed, the result will be a non-zero number, which evaluates to TRUE
Your analysis of the code is pretty much correct.
However you have not quite understood how the bitwise & operator works.
The output of & for 2 bits is given by this table
bit 2
1 0
____
bit 0|0 0
1 1|1 0
For bigger numbers the & compares the bit at the same index in each number in turn and produces an output based on table above so you only get a 1 in the output if there is a 1 in the same location in both inputs. So for your example numbers you get
00000000000000001000 //input 1
00000000000000001010 //input 2
00000000000000001000 //output
Now any non zero number equates to true so the if statement will enter the print "1" case. I prefer the more explicit code
if( (number & mask) != 0 )
Basically all the loop is doing is working through the number a bit at a time and testing if it is 1 or zero and printing the corresponding character.
I am having problem understanding how this piece of code works. I understand when the x is a positive number, actually only (x & ~mark) have a value; but cannot figure what this piece of code is doing when x is a negative number.
e.g. If x is 1100(-4), and mask would be 0001, while ~mask is 1110.
The result of ((~x & mask) + (x & ~mask)) is 0001 + 1100 = 1011(-3), I tried hard but cannot figure out what this piece of code is doing, any suggestion is helpful.
/*
* fitsBits - return 1 if x can be represented as an
* n-bit, two's complement integer.
* 1 <= n <= 32
* Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int fitsBits(int x, int n) {
/* mask the sign bit against ~x and vice versa to get highest bit in x. Shift by n-1, and not. */
int mask = x >> 31;
return !(((~x & mask) + (x & ~mask)) >> (n + ~0));
}
Note: this is pointless and only worth doing as an academic exercise.
The code makes the following assumptions (which are not guaranteed by the C standard):
int is 32-bit (1 sign bit followed by 31 value bits)
int is represented using 2's complement
Right-shifting a negative number does arithmetic shift, i.e. fill sign bit with 1
With these assumptions in place, x >> 31 will generate all-bits-0 for positive or zero numbers, and all-bits-1 for negative numbers.
So the effect of (~x & mask) + (x & ~mask) is the same as (x < 0) ? ~x : x .
Since we assumed 2's complement, ~x for negative numbers is -(x+1).
The effect of this is that if x is positive it remains unchanged. and if x is negative then it's mapped onto the range [0, INT_MAX] . In 2's complement there are exactly as many negative numbers as non-negative numbers, so this works.
Finally, we right-shift by n + ~0. In 2's complement, ~0 is -1, so this is n - 1. If we shift right by 4 bits for example, and we shifted all the bits off the end; it means that this number is representable with 1 sign bit and 4 value bits. So this shift tells us whether the number fits or not.
Putting all of that together, it is an arcane way of writing:
int x;
if ( x < 0 )
x = -(x+1);
// now x is non-negative
x >>= n - 1; // aka. x /= pow(2, n-1)
if ( x == 0 )
return it_fits;
else
return it_doesnt_fit;
Here is a stab at it, unfortunately it is hard to summarize bitwise logic easily. The general idea is to try to right shift x and see if it becomes 0 as !0 returns 1. If right shifting a positive number n-1 times results in 0, then that means n bits are enough to represent it.
The reason for what I call a and b below is due to negative numbers being allowed one extra value of representation by convention. An integer can represent some number of values, that number of values is an even number, one of the numbers required to represent is 0, and so what is left is an odd number of values to be distributed among negative and positive numbers. Negative numbers get to have that one extra value (by convention) which is where the abs(x)-1 comes into play.
Let me know if you have questions:
int fitsBits(int x, int n) {
int mask = x >> 31;
/* -------------------------------------------------
// A: Bitwise operator logic to get 0 or abs(x)-1
------------------------------------------------- */
// mask == 0x0 when x is positive, therefore a == 0
// mask == 0xffffffff when x is negative, therefore a == ~x
int a = (~x & mask);
printf("a = 0x%x\n", a);
/* -----------------------------------------------
// B: Bitwise operator logic to get abs(x) or 0
----------------------------------------------- */
// ~mask == 0xffffffff when x is positive, therefore b == x
// ~mask == 0x0 when x is negative, therefore b == 0
int b = (x & ~mask);
printf("b = 0x%x\n", b);
/* ----------------------------------------
// C: A + B is either abs(x) or abs(x)-1
---------------------------------------- */
// c is either:
// x if x is a positive number
// ~x if x is a negative number, which is the same as abs(x)-1
int c = (a + b);
printf("c = %d\n", c);
/* -------------------------------------------
// D: A ridiculous way to subtract 1 from n
------------------------------------------- */
// ~0 == 0xffffffff == -1
// n + (-1) == n-1
int d = (n + ~0);
printf("d = %d\n", d);
/* ----------------------------------------------------
// E: Either abs(x) or abs(x)-1 is shifted n-1 times
---------------------------------------------------- */
int e = (c >> d);
printf("e = %d\n", e);
// If e was right shifted into 0 then you know the number would have fit within n bits
return !e;
}
You should be performing those operations with unsigned int instead of int.
Some operations like >> will perform an arithmetic shift instead of logical shift when dealing with signed numbers and you will have this sort of unexpected outcome.
A right arithmetic shift of a binary number by 1. The empty position in the most significant bit is filled with a copy of the original MSB instead of zero. -- from Wikipedia
With unsigned int though this is what happens:
In a logical shift, zeros are shifted in to replace the discarded bits. Therefore the logical and arithmetic left-shifts are exactly the same.
However, as the logical right-shift inserts value 0 bits into the most significant bit, instead of copying the sign bit, it is ideal for unsigned binary numbers, while the arithmetic right-shift is ideal for signed two's complement binary numbers. -- from Wikipedia
How do I check if an integer is even or odd using bitwise operators
Consider what being "even" and "odd" means in "bit" terms. Since binary integer data is stored with bits indicating multiples of 2, the lowest-order bit will correspond to 20, which is of course 1, while all of the other bits will correspond to multiples of 2 (21 = 2, 22 = 4, etc.). Gratuituous ASCII art:
NNNNNNNN
||||||||
|||||||+−− bit 0, value = 1 (20)
||||||+−−− bit 1, value = 2 (21)
|||||+−−−− bit 2, value = 4 (22)
||||+−−−−− bit 3, value = 8 (23)
|||+−−−−−− bit 4, value = 16 (24)
||+−−−−−−− bit 5, value = 32 (25)
|+−−−−−−−− bit 6, value = 64 (26)
+−−−−−−−−− bit 7 (highest order bit), value = 128 (27) for unsigned numbers,
value = -128 (-27) for signed numbers (2's complement)
I've only shown 8 bits there, but you get the idea.
So you can tell whether an integer is even or odd by looking only at the lowest-order bit: If it's set, the number is odd. If not, it's even. You don't care about the other bits because they all denote multiples of 2, and so they can't make the value odd.
The way you look at that bit is by using the AND operator of your language. In C and many other languages syntactically derived from B (yes, B), that operator is &. In BASICs, it's usually And. You take your integer, AND it with 1 (which is a number with only the lowest-order bit set), and if the result is not equal to 0, the bit was set.
I'm intentionally not actually giving the code here, not only because I don't know what language you're using, but because you marked the question "homework." :-)
In C (and most C-like languages)
if (number & 1) {
// It's odd
}
if (number & 1)
number is odd
else // (number & 1) == 0
number is even
For example, let's take integer 25, which is odd.
In binary 25 is 00011001. Notice that the least significant bit b0 is 1.
00011001
00000001 (00000001 is 1 in binary)
&
--------
00000001
Just a footnote to Jim's answer.
In C#, unlike C, bitwise AND returns the resulting number, so you'd want to write:
if ((number & 1) == 1) {
// It's odd
}
if(x & 1) // '&' is a bit-wise AND operator
printf("%d is ODD\n", x);
else
printf("%d is EVEN\n", x);
Examples:
For 9:
9 -> 1 0 0 1
1 -> & 0 0 0 1
-------------------
result-> 0 0 0 1
So 9 AND 1 gives us 1, as the right most bit of every odd number is 1.
For 14:
14 -> 1 1 1 0
1 -> & 0 0 0 1
------------------
result-> 0 0 0 0
So 14 AND 1 gives us 0, as the right most bit of every even number is 0.
Also in Java you will have to use if((number&1)==1){//then odd}, because in Java and C# like languages the int is not casted to boolean. You'll have to use the relational operators to return
a boolean value i.e true and false unlike C and C++ like languages which treats non-zero value as true.
You can do it simply using bitwise AND & operator.
if(num & 1)
{
//I am odd number.
}
Read more over here - Checking even odd using bitwise operator in C
Check Number is Even or Odd using XOR Operator
Number = 11
1011 - 11 in Binary Format
^ 0001 - 1 in Binary Format
----
1010 - 10 in Binary Format
Number = 14
1110 - 14 in Binary Format
^ 0001 - 1 in Binary Format
----
1111 - 15 in Binary Format
AS It can observe XOR Of a number with 1, increments it by 1 if it is
even, decrements it by 1 if it is odd.
Code:
if((n^1) == (n+1))
cout<<"even\n";
else
cout<<"odd\n";
#include <iostream>
#include <algorithm>
#include <vector>
void BitConvert(int num, std::vector<int> &array){
while (num > 0){
array.push_back(num % 2);
num = num / 2;
}
}
void CheckEven(int num){
std::vector<int> array;
BitConvert(num, array);
if (array[0] == 0)
std::cout << "Number is even";
else
std::cout << "Number is odd";
}
int main(){
int num;
std::cout << "Enter a number:";
std::cin >> num;
CheckEven(num);
std::cout << std::endl;
return 0;
}
In Java,
if((num & 1)==0){
//its an even num
}
//otherwise its an odd num
This is an old question, however the other answers have left this out.
In addition to using num & 1, you can also use num | 1 > num.
This works because if a number is odd, the resulting value will be the same since the original value num will have started with the ones bit set, however if the original value num was even, the ones bit won't have been set, so changing it to a 1 will make the new value greater by one.
Approach 1: Short and no need for explicit comparison with 1
if (number & 1) {
// number is odd
}
else {
// number is even
}
Approach 2: Needs an extra bracket and explicit comparison with 0
if((num & 1) == 0){ // Note: Bracket is MUST around num & 1
// number is even
}
else {
// number is odd
}
What would happen if I miss the bracket in the above code
if(num & 1 == 0) { } // wrong way of checking even or not!!
becomes
if(num & (1 == 0)) { } // == is higher precedence than &
https://en.cppreference.com/w/cpp/language/operator_precedence
Suppose you have 2 numbers:
int x = 1;
int y = 2;
Using bitwise operators, how can i represent x-y?
When comparing the bits of two numbers A and B there are three posibilities. The following assumes unsigned numbers.
A == B : All of the bits are the same
A > B: The most significant bit that differs between the two numbers is set in A and not in B
A < B: The most significant bit that differs between the two numbers is set in B and not in A
Code might look like the following
int getDifType(uint32_t A, uint32_t B)
{
uint32_t bitMask = 0x8000000;
// From MSB to LSB
for (bitMask = 0x80000000; 0 != bitMask; bitMask >>= 1)
{
if (A & bitMask != B & bitMask)
return (A & bitMask) - (B & bitMask);
}
// No difference found
return 0;
}
You need to read about two's complement arithmetic. Addition, subtraction, negation, sign testing, and everything else are all done by the hardware using bitwise operations, so you can definitely do it in your C program. The wikipedia link above should teach you everything you need to know to solve your problem.
Your first step will be to implement addition using only bitwise operators. After that, everything should be easy. Start small- what do you have to do to implement 00 + 00, 01 + 01, etc? Go from there.
You need to start checking from the most significant end to find if a number is greater or not. This logic will work only for non-negative integers.
int x,y;
//get x & y
unsigned int mask=1; // make the mask 000..0001
mask=mask<<(8*sizeoF(int)-1); // make the mask 1000..000
while(mask!=0)
{
if(x & mask > y & mask)
{printf("x greater");break;}
else if(y & mask > x & mask)
{printf("y greater");break;}
mask=mask>>1; // shift 1 in mask to the right
}
Compare the bits from left to right, looking for the leftmost bits that differ. Assuming a machine that is two's complement, the topmost bit determines the sign and will have a flipped comparison sense versus the other bits. This should work on any two's complement machine:
int compare(int x, int y) {
unsigned int mask = ~0U - (~0U >> 1); // select left-most bit
if (x & mask && ~y & mask)
return -1; // x < 0 and y >= 0, therefore y > x
else if (~x & mask && y & mask)
return 1; // x >= 0 and y < 0, therefore x > y
for (; mask; mask >>= 1) {
if (x & mask && ~y & mask)
return 1;
else if (~x & mask && y & mask)
return -1;
}
return 0;
}
[Note that this technically isn't portable. C makes no guarantees that signed arithmetic will be two's complement. But you'll be hard pressed to find a C implementation on a modern machine that behaves differently.]
To see why this works, consider first comparing two unsigned numbers, 13d = 1101b and 11d = 1011b. (I'm assuming a 4-bit wordsize for brevity.) The leftmost differing bit is the second from the left, which the former has set, while the other does not. The former number is therefore the larger. It should be fairly clear that this principle holds for all unsigned numbers.
Now, consider two's complement numbers. You negate a number by complementing the bits and adding one. Thus, -1d = 1111b, -2d = 1110b, -3d = 1101b, -4d = 1100b, etc. You can see that two negative numbers can be compared as though they were unsigned. Likewise, two non-negative numbers can also be compared as though unsigned. Only when the signs differ do we have to consider them -- but if they differ, the comparison is trivial!