Bitwise operations in gimkit

FYI: The most replies on a topic was an updates post made by Josh, having around 4,700 replies (?) before it got deleted.

1 Like

This does not. If you were to scroll up and read them, you’ll find that almost all of them are on topic discussing math and bitwise. High reply counts are not inherently bad.

2 Likes

about 900-1000+ replies

1 Like

bump because unresolved maths

1 Like

*semi unresolved maths
The entire post is irrelevant due to the text operations update, and really just exists as a fun math problem to solve. Getrithekd actually found a semi-solution that worked in just the cases I needed it for, but it doesn’t work for everything, so the problem remains open.

1 Like

yeah i should probably read all 750 replies before saying something :eyes:

2 Likes

hi I just sent this in discord
just divide by 2^n(right shift n) and check if result is 1 mod 2
assuming integer/integer gives integer rounded down
I need to afk go to class, msg me on discord

1 Like

Alright, for those not on discord, eesoog provided a similar solution to getrithekd’s. His other idea which is interesting is that you could precompute tables of answers for the bitwise AND of different numbers, which, although it involves loops still, would be interesting to think about.

1 Like

Screenshot 2024-04-19 11.50.26 AM

2 Likes

That’s pretty much what this is now haha.

2 Likes

I’m actually trying to prove a non-looping solution impossible now.

2 Likes

Good luck!

2 Likes

im laughing my apple off rn :rofl:

classic post by blackhole :rofl:

1 Like

I got this from ChatGPT:

def bitwise_and_with_power_of_2(number, n):
    # Check if the bit at the n-th position of 2^n exists in the number
    if (number >> n) & 1:
        return 2**n
    else:
        return 0

# Example usage
number = 2863311530
n = 0
result = bitwise_and_with_power_of_2(number, n)
print(result)  # Output will be 1

1 Like

Btw the code is python. idk how to translate it to Gimkit coding.

2 Likes

oh yeah, carrot is also why I am here

1 Like

Let’s break down how the bitwise_and_with_power_of_2 function works:

  1. Function Purpose: This function checks whether a specific bit (corresponding to 2𝑛2n) is set (i.e., equals 1) in the binary representation of a given number.
  2. Function Logic:
  • number >> n: This shifts the binary representation of number to the right by n positions. This effectively isolates the bit at the n-th position.
  • & 1: This performs a bitwise AND operation with 1, which checks whether the isolated bit is 1 (if the result is non-zero) or 0 (if the result is zero).
  1. Return Value:
  • If the n-th bit of number is 1, the function returns 2𝑛2n.
  • If the n-th bit of number is 0, the function returns 0.
  1. Example Usage: Let’s analyze the example provided:

python

Copy code

number = 2863311530  # Binary: 10101010101010101010101010101010
n = 0
result = bitwise_and_with_power_of_2(number, n)
print(result)  # Output will be 1
  • number in binary: 10101010101010101010101010101010
  • Bit at position n=0: 0
  • (number >> n) & 1 evaluates to (2863311530 >> 0) & 1, which simplifies to 2863311530 & 1.
  • 2863311530 & 1 equals 0.
  • Therefore, the function returns 0 because the bit at position n=0 of number is 0.
  1. Summary:
  • The function bitwise_and_with_power_of_2 is designed to check if a specific bit in the binary representation of a number is set and returns the corresponding power of 2 if true, or 0 if false.

In the provided example, since the bit at position n=0 of number is 0, the output of result is 0. If you change n to 1, 2, etc., you can check different bit positions accordingly. is what chat gpt said

1 Like

imo while loops are better, but for loops are insanely useful too. (also I’m back yay :slightly_smiling_face:)

1 Like

Oh yeah I forgot to mention that I needed this for my 3D renderer lol, I don’t think I ever said that.

2 Likes

I’m-back-bump!

9 Likes