Introduction to Bitwise Operators 11/14 input or inpt, integer or binary?


#1

Instructions:

Define a function, check_bit4 , with one argument, input , an integer.

It should check to see if the fourth bit from the right is on.

If the bit is on, return "on" (not print !)

If the bit is off, return "off" .

Check the Hint for some examples!

Here are some examples:

check_bit4(0b1) # ==> "off"
check_bit4(0b11011) # ==> "on"
check_bit4(0b1010) # ==> "on"

You’ll need to use a mask where all bits are off except for the fourth bit from the right.

My code:

def check_bit4(inpt):
  num = inpt
  mask = 0b1000
  desired = num & mask
  if desired > 0:
    return "on"
  else:
    return "off"

check_bit4(0b1101)

Codecademy solution:

def check_bit4(input):
  mask = 0b1000
  desired = input & mask
  if desired > 0:
    return "on"
  else:
    return "off"

When you use input as argument, like the Codecademy solution, it turns white…maybe its not a good idea to do that? Maybe its better to write something else, like inpt?

And Codecademy doesnt call their function…should we call it with an integer like they ask us in the instructions, with lets say 13, or should we call it with a binary number instead, lets say 13 in binary (0b1101)?
It seems that both work when i try, so maybe it doesnt matter? Or maybe 13 in binary(ob1101) is already considered as an integer and thats why both ways work?


#2

yea, input is a built-in function, so its not ideal.

in your case, just make num the parameter, then you can skip num = inpt step

it does behind the scenes, for validation purposes.

0b1101 is the binary representation of 13, so yea, both should work fine. You could just write a for in range loop and use bin to test for a whole bunch of integer and binary numbers?

so something like:

for i in range(50):
   print check_bit4(i)
   print check_bit4(bin(i))

bin() will convert integer to binary for us :slight_smile:

for any number (so both integer and binary) it should give the same result.


#3

exactly :slight_smile:

Yes, basically the Codecademy solution, but use num instead of input

Oh it does?! I thought you allways have to call a function for it to return something…

Awesome, then I’ll write it down in my notes that both work :wink:

Exactly exactly


#4

yes, but look at this:

https://repl.it/@stetim94/SandybrownDeepskyblueTahr

there are two files, script.py and main.py. Lets say script.py is your exercise code, then codecademy can extract the function and call the function in a different script to validate your code (if you should pass the exercise)

Codecademies implementation is very likely more sophisticated, but my example shows how you can execute a function from a different script. So codecademy does call your function

and think about it, calling functions defined in a different script is something which you have already done:

from math import factorial

so then we import the factorial function defined in math.py


#5

Yes I see what you mean


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.