Simulating bitwise AND operator

  • Thread starter Thread starter Puzzle
  • Start date Start date
  • Tags Tags
    Operator
Click For Summary

Discussion Overview

The discussion revolves around finding a method to extract the value of a specific binary bit from a decimal number without using bitwise operators, particularly in a constrained environment that only allows certain mathematical operations. Participants explore various approaches and challenges related to this problem.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes a function to extract bit values from a decimal number using bitwise operators and seeks an alternative method that avoids them.
  • Another participant suggests adapting a method of converting decimal to binary by dividing the integer and analyzing the fractional part to determine bit values.
  • A third participant proposes a formula involving multiplication and division to extract bit values, assuming unsigned integer math with 32-bit numbers.
  • There is a concern raised about the limitations of using high precision floating point values, which complicates the implementation of the proposed methods.
  • One participant questions how to perform a modulo operation with the specified mathematical tools, expressing doubt about achieving this without a floor function.
  • Another participant mentions the potential implications of using floating point numbers in a binary coded decimal format, raising concerns about the feasibility of the proposed solutions.

Areas of Agreement / Disagreement

Participants express differing views on the feasibility of extracting bit values without bitwise operations, with some proposing mathematical approaches while others highlight limitations and challenges. The discussion remains unresolved regarding the best method to achieve the goal.

Contextual Notes

Participants note the constraints of using high precision floating point values and the specific mathematical operations allowed, which may limit the effectiveness of proposed solutions. There is also uncertainty regarding the handling of floating point numbers in different formats.

Puzzle
Messages
2
Reaction score
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
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.
 
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)
 
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?
 
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:

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
7
Views
2K
  • · Replies 68 ·
3
Replies
68
Views
13K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
3K