reverse a given number only using bitwise operation
e.g.:
input: 4532
output : 2354
I'm not able to think of any bitwise operations to tackle this question.Any solution/assistance would be of great help.
The division and modulo by 10 needs to be performed using bitwise operations only.
there's no efficient way of reversing a number using only bitwise operators (mostly shift operators). But we can use BCD conversion to convert the integer to BCD and then reverse using shift operators. So after BCD conversion we can shift every set of 4 bits (representing a digit ranging from 0-9) step wise in the opposite order, to get the reversed Number in BCD format.
Related
Can the bitwise AND of some positive integers be negative?
Let's say I have some numbers 1,2,3..N. For example, take N=7 and I want to find a subarray which results me in a negative result after bitwise AND operation.
Taking 4,5,6,7 gives me 4(100) but what subarray(for any N) could give me negative result?
(In most/commonly used encodings) sign is stored in first bit.
For first bit to be 1 (meaning negative number) after AND, both bits need to be 1 before that. So it means only 2 negative numbers can make a negative number with AND.
This is true both for integers and floats (IEEE754) (the most common int and float implementations) but basically anything that can store negative numbers has to store the sign somewhere... and as positive numbers are the "default", it's minus that gets marked as 1.
Can the bitwise AND of some positive integers be negative?
No, it can't.
A negative number has the top bit set. All positive numbers have the top bit unset. A zero bit AND a zero bit results in a zero bit. This applies to any number of bitwise AND operations performed on any number of positive integers.
And it also applies if you include zero.
In fact, if you perform an bitwise AND on a collection of integers, the result will only be negative if all of the integers are less than zero.
Note that above is for twos' complement binary representations. All modern mainstream computer instruction sets use twos' complement to represent integers.
With ones' complement you actually have two zeros +0 and -0. But if you treat -0 as negative then the same logic applies.
Bitwise AND'ing of floating point number representations ... and treating the results as numbers ... doesn't make a lot of sense. So I haven't considered that.
How can I reverse a number without using arrays or any arithmetic operations i.e from 85 to 58. Using bitwise operators might be the solution. But what series of operations are needed to reverse a number. I've tried shifting and complementing.
And is there a way to get binary or hexadecimal as input and perform operations on it. Rather than getting int and typecast at printf?
there are alot of api's to do that // but the easiest way use a stack :D or convert it to string then it will be such as array you can inverse it easilly
I found the answer after all. Using the right shift 8 times and left shift 2 times would work on a 2 digit number. And follow the shift bits for different digit numbers,
I'm working on a C++ library to manage very large value masks (each 2^32 bits, about .5 GB). Each value mask will represent possible values of an 32-bit int. For example, 00101000 would represent the values 2 and 4. Each value mask will contain mostly repetitive data (example: 01010000010100000101000001010000).
I'm looking for some kind of compression algorithm that allows somewhat-easy bitwise operations on the values of two value masks. Clarification: I don't want to do bitwise operations on the value mask itself, but the values. See example below:
ValMask mask_a(0b00101000); // Represents 2 and 4
ValMask mask_b(0b10110000); // Represents 0, 2 and 3
// Sum all of the values of mask_a and mask_b
ValMask mask_c = mask_a + mask_b;
// mask_c is 0b00101111, which represents 2, 4, 5, 6, 7
My class will include these operators: +, -, &, |, ^, ~, <<, and >>
Obviously, it would be really nice if I could calculate the result of some of these operators without decompressing the value mask. I doubt any compression algorithm can do all of those (challenge, anyone?), but is there are any existing libraries or algorithms for something like this?
I know that this operator does the "AND logical operator" but I don't know how to deal with it and if it deals with decimal numbers or just binary numbers ?
It is the the XOR operator:
XOR (Exclusive Or)
This operation is performed between two bits (a and b). The result is
1 if either one of the two bits is 1, but not in the case that both
are. There for, if neither or both of them are equal to 1 the result
is 0.
That is the bitwise XOR operator.
It performs an exclusive or operation.
It is not the logical AND, it's the bitwise XOR. It operates on integers, which are always binary numbers in C++, and may be overloaded for other types.
That's the bitwise XOR operator (so not logical, not AND) and decimal numbers are represented as binaries, so of course it works for them as well.
The logical AND is && and the bitwise AND is &.
Lets say a = 0x6db7 and b = 0x736.How to compute a&b and a|b manually?
I am aware of bitwise operations,and I know this can be solved by converting a and b into it's binary form and then bitwise operations and then again converting to hex,however what I am looking for is a solution that doesn't involve too much of calculation without the interdemidiate binary conversion.
Is it possible ?
Those are bitwise and and bitwise or. To compute them manually, convert each number to binary, then compute the result. The result of an and will be 1 if and only if the corresponding bits are set. The result of an or will be 1 if the corresponding bits in either one or the other or both are set:
100111 100111
&110010 |110010
------- -------
100010 110111
After that, you normally want to convert the result back to some other base (e.g., hexadecimal).