r/dailyprogrammer • u/oskar_s • Jun 20 '12
[6/20/2012] Challenge #67 [easy]
As we all know, when computers do calculations or store numbers, they don't use decimal notation like we do, they use binary notation. So for instance, when a computer stores the number 13, it doesn't store "1" and "3", it stores "1101", which is 13 in binary.
But more than that, when we instruct it to store an integer, we usually tell it to store it in a certain datatype with a certain length. For (relatively small) integers, that length is usually as 32 bits, or four bytes (also called "one word" on 32-bit processors). So 13 isn't really stored as "1101", it's stored as "00000000000000000000000000001101".
If we were to reverse that bit pattern, we would get "10110000000000000000000000000000", which written in decimal becomes "2952790016".
Write a program that can do this "32-bit reverse" operation, so when given the number 13, it will return 2952790016.
Note: just to be clear, for all numbers in this problem, we are using unsigned 32 bit integers.
- Thanks to HazzyPls for suggesting this problem at /r/dailyprogrammer_ideas! Do you have a problem you think would be good for us? Why not head over there and suggest it?
10
u/[deleted] Jun 20 '12
C:
9
u/scurvebeard 0 0 Jun 21 '12
As a complete beginner who is just starting to learn JavaScript, I am terrified by this challenge and now this entire subreddit. I thought I might be able to do this one, with a lot of work and a ton of lines, 50 at least. Now I see people doing it in 4-7 lines, some even doing it in 1.
Jesus damn but I've got a long ways to go.
4
u/sch1zo Jun 20 '12
C++
3
u/bh3 Jun 20 '12
C:
1
u/Jatak 0 0 Jun 20 '12
In Python 3.2.3
5
u/Xadoo 0 0 Jun 20 '12 edited Jun 20 '12
Ruby:
Edit: Woo, my first one-liner. /perl-envy
2
u/robotfarts Jun 20 '12
2
u/systemUp Jun 20 '12
C++
2
u/Starcast Jun 20 '12
Ruby
2
u/zane17 Jun 20 '12 edited Jun 20 '12
Haskell:
This is my first reply, I would greatly appreciate feedback
edit: formatting
2
u/ssk Jun 22 '12
In Ruby.
2
u/quirk Jul 12 '12
I know I'm late to the party, but I wanted to represent PHP.
3
u/[deleted] Jun 20 '12 edited Jun 20 '12
Java:
2
u/xjtian Jun 20 '12
Python:
2
u/Arthree Jun 20 '12 edited Jun 20 '12
Autohotkey:
1
u/MintyPhoenix Jun 20 '12
Python:
1
u/devilsassassin Jun 21 '12
Java (without bitwise operations):
1
u/huck_cussler 0 0 Jun 21 '12
Since Java doesn't support unsigned ints I had to use doubles. I decided to separate the problem out into two methods for clarity and simplicity.
1
u/funny_falcon Jun 21 '12 edited Jun 22 '12
Without cycles or conditions, only bit arithmetics (this sample in Ruby , but could be almost in any language)
1
u/kohjingyu 0 0 Jun 21 '12
Python:
1
u/name_censored_ Jun 21 '12
Bash (GNU sed, bc, echo)
Not super efficient, but I believe it takes multiple lines.
1
u/Hazger 0 0 Jun 21 '12
My solution as a beginner, have lots of methods to test if everything is working and print the numbers
C#
1
u/SangriaSunrise Jun 21 '12
J:
1
u/[deleted] Jun 22 '12
Perl. Couldn't find a way to combine the statements...
1
u/Nohsk Jun 23 '12 edited Jun 23 '12
C:
1
u/jesyspa Jun 24 '12
Another C++ solution:
Works for i not a power of 2, too. I wanted to add an assert for the value really being in the specified range, but this causes a warning for i = 32.
1
u/Erocs Jun 26 '12
Python 2.6
1
u/juror-number-8 Jul 05 '12
Ruby:
1
u/NattyBroh Jul 13 '12
Python:
1
u/cdelahousse Jul 28 '12
Javascript
1
u/paininthebass Jul 31 '12
c++:
1
u/SwimmingPastaDevil 0 0 Jun 20 '12 edited Jun 20 '12
Python:
Edit: Managed to cram in a 1-liner.
0
u/emcoffey3 0 0 Jun 20 '12
C#