Reversing the bits of a 32-bit integer

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












Here is my code:



noTest=int(input())
reverse=
nums=
for i in range(noTest):
nums.append(int(input()))

for num in nums:
binary=bin(num)
binNum=binary[2:]
rev=binNum[::-1]
rev=binary[:2]+rev+'0'*(32-len(rev))
#print int(rev,2)
reverse.append(int(rev,2))

for r in reverse:
print(r)


Input:

The first line of input consists number of the test cases. Each test case

contains a single 32 bit integer.



Output:

Print the reverse of integer.



Input:00000000000000000000000000000001 =1
Output:10000000000000000000000000000000 =2147483648






share|improve this question





















  • I have... to what purpose? Know bit hacks?
    – greybeard
    Jan 4 at 10:00
















up vote
1
down vote

favorite












Here is my code:



noTest=int(input())
reverse=
nums=
for i in range(noTest):
nums.append(int(input()))

for num in nums:
binary=bin(num)
binNum=binary[2:]
rev=binNum[::-1]
rev=binary[:2]+rev+'0'*(32-len(rev))
#print int(rev,2)
reverse.append(int(rev,2))

for r in reverse:
print(r)


Input:

The first line of input consists number of the test cases. Each test case

contains a single 32 bit integer.



Output:

Print the reverse of integer.



Input:00000000000000000000000000000001 =1
Output:10000000000000000000000000000000 =2147483648






share|improve this question





















  • I have... to what purpose? Know bit hacks?
    – greybeard
    Jan 4 at 10:00












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Here is my code:



noTest=int(input())
reverse=
nums=
for i in range(noTest):
nums.append(int(input()))

for num in nums:
binary=bin(num)
binNum=binary[2:]
rev=binNum[::-1]
rev=binary[:2]+rev+'0'*(32-len(rev))
#print int(rev,2)
reverse.append(int(rev,2))

for r in reverse:
print(r)


Input:

The first line of input consists number of the test cases. Each test case

contains a single 32 bit integer.



Output:

Print the reverse of integer.



Input:00000000000000000000000000000001 =1
Output:10000000000000000000000000000000 =2147483648






share|improve this question













Here is my code:



noTest=int(input())
reverse=
nums=
for i in range(noTest):
nums.append(int(input()))

for num in nums:
binary=bin(num)
binNum=binary[2:]
rev=binNum[::-1]
rev=binary[:2]+rev+'0'*(32-len(rev))
#print int(rev,2)
reverse.append(int(rev,2))

for r in reverse:
print(r)


Input:

The first line of input consists number of the test cases. Each test case

contains a single 32 bit integer.



Output:

Print the reverse of integer.



Input:00000000000000000000000000000001 =1
Output:10000000000000000000000000000000 =2147483648








share|improve this question












share|improve this question




share|improve this question








edited Jan 4 at 18:52









200_success

124k14143401




124k14143401









asked Jan 4 at 9:20









katty

1805




1805











  • I have... to what purpose? Know bit hacks?
    – greybeard
    Jan 4 at 10:00
















  • I have... to what purpose? Know bit hacks?
    – greybeard
    Jan 4 at 10:00















I have... to what purpose? Know bit hacks?
– greybeard
Jan 4 at 10:00




I have... to what purpose? Know bit hacks?
– greybeard
Jan 4 at 10:00










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










To expand on Caridorc's answer, you can merge bin and zfill when using str.format. Which gives a much cleaner read IMO:



int(':032b'.format(num)[::-1], 2)


Alternately you can use format, which would be:



int(format(num, '032b')[::-1], 2)





share|improve this answer



















  • 1




    int(format(num, '032b')[::-1], 2)
    – Veedrac
    Jan 4 at 17:29

















up vote
3
down vote













You can avoid building a list of results to print them after, you can just print them as you calculate them.



Also you assign a lot of variables and:



binNum=binary[2:] 


is not explicit in removing the '0b' that Python prefixes, better is



.replace('0b','')


Also the padding with 0 logic that you write is already implemented in the zfill function, so you can avoid re-writing it yourself.



Finally, you should write a function to read the input to separate concerns and maybe reuse it in a similar problem.



Here are all my suggestions implemented.



def read_numbers(n):
return [int(input()) for _ in range(n)]

for i in read_numbers( int(input()) ):
binary_padded = bin(i).replace('0b','').zfill(32)
print( int( binary_padded[::-1], 2) )





share|improve this answer

















  • 3




    replace is worse since it does a search and traverses the whole string.
    – Veedrac
    Jan 4 at 17:30










  • @Veedrac I think you are right, probably in this case [:2] should be used and maybe just adding a comment to explain the removal of '0b'
    – Caridorc
    Jan 5 at 14:06










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184261%2freversing-the-bits-of-a-32-bit-integer%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










To expand on Caridorc's answer, you can merge bin and zfill when using str.format. Which gives a much cleaner read IMO:



int(':032b'.format(num)[::-1], 2)


Alternately you can use format, which would be:



int(format(num, '032b')[::-1], 2)





share|improve this answer



















  • 1




    int(format(num, '032b')[::-1], 2)
    – Veedrac
    Jan 4 at 17:29














up vote
3
down vote



accepted










To expand on Caridorc's answer, you can merge bin and zfill when using str.format. Which gives a much cleaner read IMO:



int(':032b'.format(num)[::-1], 2)


Alternately you can use format, which would be:



int(format(num, '032b')[::-1], 2)





share|improve this answer



















  • 1




    int(format(num, '032b')[::-1], 2)
    – Veedrac
    Jan 4 at 17:29












up vote
3
down vote



accepted







up vote
3
down vote



accepted






To expand on Caridorc's answer, you can merge bin and zfill when using str.format. Which gives a much cleaner read IMO:



int(':032b'.format(num)[::-1], 2)


Alternately you can use format, which would be:



int(format(num, '032b')[::-1], 2)





share|improve this answer















To expand on Caridorc's answer, you can merge bin and zfill when using str.format. Which gives a much cleaner read IMO:



int(':032b'.format(num)[::-1], 2)


Alternately you can use format, which would be:



int(format(num, '032b')[::-1], 2)






share|improve this answer















share|improve this answer



share|improve this answer








edited Jan 4 at 18:17


























answered Jan 4 at 16:37









Peilonrayz

24.4k336102




24.4k336102







  • 1




    int(format(num, '032b')[::-1], 2)
    – Veedrac
    Jan 4 at 17:29












  • 1




    int(format(num, '032b')[::-1], 2)
    – Veedrac
    Jan 4 at 17:29







1




1




int(format(num, '032b')[::-1], 2)
– Veedrac
Jan 4 at 17:29




int(format(num, '032b')[::-1], 2)
– Veedrac
Jan 4 at 17:29












up vote
3
down vote













You can avoid building a list of results to print them after, you can just print them as you calculate them.



Also you assign a lot of variables and:



binNum=binary[2:] 


is not explicit in removing the '0b' that Python prefixes, better is



.replace('0b','')


Also the padding with 0 logic that you write is already implemented in the zfill function, so you can avoid re-writing it yourself.



Finally, you should write a function to read the input to separate concerns and maybe reuse it in a similar problem.



Here are all my suggestions implemented.



def read_numbers(n):
return [int(input()) for _ in range(n)]

for i in read_numbers( int(input()) ):
binary_padded = bin(i).replace('0b','').zfill(32)
print( int( binary_padded[::-1], 2) )





share|improve this answer

















  • 3




    replace is worse since it does a search and traverses the whole string.
    – Veedrac
    Jan 4 at 17:30










  • @Veedrac I think you are right, probably in this case [:2] should be used and maybe just adding a comment to explain the removal of '0b'
    – Caridorc
    Jan 5 at 14:06














up vote
3
down vote













You can avoid building a list of results to print them after, you can just print them as you calculate them.



Also you assign a lot of variables and:



binNum=binary[2:] 


is not explicit in removing the '0b' that Python prefixes, better is



.replace('0b','')


Also the padding with 0 logic that you write is already implemented in the zfill function, so you can avoid re-writing it yourself.



Finally, you should write a function to read the input to separate concerns and maybe reuse it in a similar problem.



Here are all my suggestions implemented.



def read_numbers(n):
return [int(input()) for _ in range(n)]

for i in read_numbers( int(input()) ):
binary_padded = bin(i).replace('0b','').zfill(32)
print( int( binary_padded[::-1], 2) )





share|improve this answer

















  • 3




    replace is worse since it does a search and traverses the whole string.
    – Veedrac
    Jan 4 at 17:30










  • @Veedrac I think you are right, probably in this case [:2] should be used and maybe just adding a comment to explain the removal of '0b'
    – Caridorc
    Jan 5 at 14:06












up vote
3
down vote










up vote
3
down vote









You can avoid building a list of results to print them after, you can just print them as you calculate them.



Also you assign a lot of variables and:



binNum=binary[2:] 


is not explicit in removing the '0b' that Python prefixes, better is



.replace('0b','')


Also the padding with 0 logic that you write is already implemented in the zfill function, so you can avoid re-writing it yourself.



Finally, you should write a function to read the input to separate concerns and maybe reuse it in a similar problem.



Here are all my suggestions implemented.



def read_numbers(n):
return [int(input()) for _ in range(n)]

for i in read_numbers( int(input()) ):
binary_padded = bin(i).replace('0b','').zfill(32)
print( int( binary_padded[::-1], 2) )





share|improve this answer













You can avoid building a list of results to print them after, you can just print them as you calculate them.



Also you assign a lot of variables and:



binNum=binary[2:] 


is not explicit in removing the '0b' that Python prefixes, better is



.replace('0b','')


Also the padding with 0 logic that you write is already implemented in the zfill function, so you can avoid re-writing it yourself.



Finally, you should write a function to read the input to separate concerns and maybe reuse it in a similar problem.



Here are all my suggestions implemented.



def read_numbers(n):
return [int(input()) for _ in range(n)]

for i in read_numbers( int(input()) ):
binary_padded = bin(i).replace('0b','').zfill(32)
print( int( binary_padded[::-1], 2) )






share|improve this answer













share|improve this answer



share|improve this answer











answered Jan 4 at 16:12









Caridorc

22.6k432111




22.6k432111







  • 3




    replace is worse since it does a search and traverses the whole string.
    – Veedrac
    Jan 4 at 17:30










  • @Veedrac I think you are right, probably in this case [:2] should be used and maybe just adding a comment to explain the removal of '0b'
    – Caridorc
    Jan 5 at 14:06












  • 3




    replace is worse since it does a search and traverses the whole string.
    – Veedrac
    Jan 4 at 17:30










  • @Veedrac I think you are right, probably in this case [:2] should be used and maybe just adding a comment to explain the removal of '0b'
    – Caridorc
    Jan 5 at 14:06







3




3




replace is worse since it does a search and traverses the whole string.
– Veedrac
Jan 4 at 17:30




replace is worse since it does a search and traverses the whole string.
– Veedrac
Jan 4 at 17:30












@Veedrac I think you are right, probably in this case [:2] should be used and maybe just adding a comment to explain the removal of '0b'
– Caridorc
Jan 5 at 14:06




@Veedrac I think you are right, probably in this case [:2] should be used and maybe just adding a comment to explain the removal of '0b'
– Caridorc
Jan 5 at 14:06












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184261%2freversing-the-bits-of-a-32-bit-integer%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation