Skip to content

Instantly share code, notes, and snippets.

@naimrajib07
Last active August 29, 2015 14:23
Show Gist options
  • Save naimrajib07/8e15a4037edd2ae2bf5f to your computer and use it in GitHub Desktop.
Save naimrajib07/8e15a4037edd2ae2bf5f to your computer and use it in GitHub Desktop.
Number Of Bit Count From A Image
# @params img_file String
# @return number of 0 and 1
#
# irb =>
# => require '/home/naim/Desktop/bit_counter.rb'
# => count_bits '/home/naim/Desktop/SAM_1247.jpg'
#
def count_bits img_file
img_file = File.read img_file
img_binary_string = img_file.unpack("B*").to_s
number_of_ones = img_binary_string.count("1")
number_of_zeros = img_binary_string.count("0")
puts "found #{number_of_ones} bits set to 1"
puts "found #{number_of_zeros} bits set to 0"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment