Simulating bitwise AND operator

  • Thread starter Puzzle
  • Start date
  • Tags
    Operator
In summary, the conversation discusses a problem of retrieving binary bit values from a decimal number using bitwise operators, but the specific application only allows for basic arithmetic operations and mathematical functions with floating point decimal numbers. One possible solution involves dividing the decimal number by 2 and checking the fractional part to determine the bit value, but this may not work with high precision floating point values. Another obstacle is figuring out how to perform a modulo operation without a floor() function.
  • #1
Puzzle
2
0
Hello,

I have a problem where I must get value of a given binary bit in decimal number:

f(decimal_number, bit_position) = bit_value

For example, getting bit values in decimal 14 (binary 1110):

f(14, 0) = 0
f(14, 1) = 1
f(14, 2) = 1
f(14, 3) = 1

In general it's easy:

(14 & 20) >> 0 = 0
(14 & 21) >> 1 = 1
(14 & 22) >> 2 = 1
(14 & 23) >> 3 = 1

& is bitwise AND
>> is bitwise arithmetic shift right

But in specific (technical) application I cannot use bitwise operators :(
So far I managed to get rid of one of them:

(14 & 20) / (20) = 0
(14 & 21) / (21) = 1
(14 & 22) / (22) = 1
(14 & 23) / (23) = 1

But how to get rid of bitwise AND operator?

Specific application limits me to following things to play with:
+, -, *, /, (, ), square root, xy, ex, ln, log, abs, sin, cos, tan, asin, acos, atan
using floating point decimal numbers. And it has to be "single line" - cannot divide solution
to parts, use variables besides given decimal_number or use any if/else etc logic.

It does not have to be "nice" solution, any "hack" will do as long as it works. For example
"helper" constants (specific to bit position) could be used as additional input to the function.
 
Last edited:
Physics news on Phys.org
  • #2
You may be able to adapt the "keep dividing by 2" method for converting decimal to binary.

To see whether there is an 8 (i.e., ––1–––) divide your integer by 16. If the fractional part is >.5 the 3rd bit is a 1, i.e., a 1 in the 23 column.
 
  • #3
If this is unsigned integer math, using 32 bit numbers, with no overflow check:

d = decimal number
b = bit number

f(d,b) = (d × 231-b) / (231)
 
  • #4
rcgldr said:
f(d,b) = (d × 231-b) / (231)

Thats a good way yes, but sadly seems that my "black box" is operating using high precision floating point values :( I think I could still implement what I need if one last obstacle could be overcome:

How to do modulo operation (reminder of division) with toolset that I mentioned?
 
  • #5
rcgldr said:
f(d,b) = (d × 231-b) / (231)

Puzzle said:
Thats a good way yes, but sadly seems that my "black box" is operating using high precision floating point values.
I missed the part about floating point decimal numbers.

Puzzle said:
How to do modulo operation (reminder of division) with toolset that I mentioned?
I don't see how this could be done without a floor() function. You mentioned decimal point floating numbers, which could mean BCD (binary coded decimal) strings used for "packed decimal" in Cobol. This would be a problem. If the floating point numbers were binary based, and you knew the format and how hardware handled truncation, you could add, then subtract, a large power of 2 to remove the lower bits, but that still leaves upper bits to be cleared.
 
Last edited:

1. What is a bitwise AND operator?

The bitwise AND operator is a logical operator used in computer programming to perform a bitwise comparison between two numbers. It compares the binary representation of each number and returns a new number where each bit is set to 1 only if both corresponding bits in the original numbers are also 1.

2. How is a bitwise AND operator simulated?

A bitwise AND operator can be simulated using the logical AND operator (&) in most programming languages. This operator performs the same bitwise comparison as the bitwise AND operator and is more commonly used in code.

3. What is the purpose of simulating a bitwise AND operator?

The purpose of simulating a bitwise AND operator is to perform bitwise operations on numbers that are not supported by the language's built-in bitwise operators. This allows for more flexibility in programming and can be used to optimize certain algorithms.

4. Can the simulated bitwise AND operator be used on non-numeric data types?

Yes, the simulated bitwise AND operator can be used on non-numeric data types as long as the data can be represented in binary form. However, the results may not always be meaningful or expected, so caution should be taken when using it on non-numeric data types.

5. Are there any limitations to simulating a bitwise AND operator?

One limitation of simulating a bitwise AND operator is that it can be more computationally expensive compared to using the built-in bitwise AND operator. Additionally, depending on the programming language and data type, the results may not always be accurate or expected. It is important to understand the language's specific implementation of the operator before using it in code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Nuclear Engineering
Replies
7
Views
2K
  • Precalculus Mathematics Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Replies
68
Views
9K
Replies
7
Views
843
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Replies
3
Views
2K
  • Nuclear Engineering
Replies
7
Views
535
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Back
Top