![]() |
|
Coding Contest - Rotating Bits - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5) +---- Thread: Coding Contest - Rotating Bits (/showthread.php?tid=83701) |
Coding Contest - Rotating Bits - g4143 - Jun 2, 2009 Create a program/function that will rotate the bits of a unsigned integer by n times in the left or right direction... So what is rotating bits, well lets look at an example:🤮 Say we have a binary number 0110 0101 and we want to rotate this number to the right by two. First thing we do is shift the number to the right by two pushing two numbers off 0110 01 - 01 then we take those two numbers 01 and place then on the left of the binary number so the result is 0101 1001. So you can see anything we push off the right side is picked up and placed on the left. Its kind of like a conveyor belt with bits. Now to rotate to the left we just reverse the process... So to return to the task - create a program/function that will rotate a unsigned integer to the left or right n times... Note it will help if you have a calculator that displays numbers and binary...G4143 This is the last one...Its officially someone else's turn to come with the next task or competition - JapaBrz - Jun 2, 2009 in vb.net Code Module Module1Calls getbinary(value as uinteger) as string converts the uinteger value to a binary string Rotate(ByVal Binary As String, ByVal N As Integer, ByVal dLeft As Boolean,nAmount as integer) As String takes a binary string, rotate N times to left(is true) or right(if false) and returns the binary string wanna convert back to uinteger, namount is the reversed chunk size [edited] this function converts back to uinteger Code Public Function getUInteger(ByVal Binary As String) As UIntegerthe runcode ill be getUInteger(Rotate(GetBinary(121), 1, False, 2)) 01111001 11100101 returns this - g4143 - Jun 2, 2009 japabrz, I don't have VB so I have no idea if you program is right or wrong so I'll provide you with a number: 3222269959 if you rotate this to the right 4 bits you should arrive at a number: 2080440064 This is assuming my solution is correct...G4143 - g4143 - Jun 3, 2009 What only one taker?...G4143 - g4143 - Jun 3, 2009 The solution - or my solution...G4143 Code #include <stdio.h>- JapaBrz - Jun 3, 2009 my version only works with unassigned 16bit integer values. what it does: -convert the integer to binary -gets the last N amount of bits from right and puts to left or gets the last N amount of bits from left and puts to right Ni times -converts back to uinteger - g4143 - Jun 3, 2009 (Jun 3, 2009 05:10 PM)japabrz Wrote: -convert the integer to binary Now I know this is nick picking but the number is already in binary. The software just displays it in a format we are more comfortable with, say decimal, so you really don't have to convert it just apply the proper operators/functions to it since its conveniently already in binary...G4143 - JapaBrz - Jun 3, 2009 omg i know that 00000001=1 and 10000000=128 -_- 32bit 16 8 , etc. im saying what the function i wrote uses a different method cuz i dont find any other way of moving the last bits of a uint value in vbnet without converting to binary first. i know is possible to with some other operation but since i already have a binary function(that i wrote) i use it to save time so lets say that i mean is "Translate" the integer to Binary ^^ - g4143 - Jun 3, 2009 (Jun 3, 2009 05:35 PM)japabrz Wrote: omg i know that 00000001=1 and 10000000=128 -_- This is one of the reasons I love C. Everything is out in the open, the walls of abstraction are very thin here, actually see through in most cases...G4143 Note I'm not saying that C is a better language because it can be horrendous at times. Its just where I have to be in programming - in control, maybe after a few more years with low/mid-level languages I'll be comfortable enough to make the step to a higher level language..G4143 - scaryblackguy - Jun 3, 2009 (Jun 3, 2009 05:47 PM)g4143 Wrote: maybe after a few more years with low/mid-level languages I'll be comfortable enough to make the step to a higher level language..G4143 lol. This is the most backwards statement ever. However, I think more people should think like you. Learn how the computer works with low level programming such as C and ASM, and then move up to the higher level languages once you know how the functions are actually working. This probably makes your code more efficient and powerful. I wish I took the same programming route as you. - g4143 - Jun 3, 2009 (Jun 3, 2009 09:21 PM)scaryblackguy Wrote: lol. This is the most backwards statement ever. However, I think more people should think like you. Learn how the computer works with low level programming such as C and ASM, and then move up to the higher level languages once you know how the functions are actually working. This probably makes your code more efficient and powerful. I wish I took the same programming route as you. One thing a lot of people do is assume that since you can program you understand how computers works. Programming is just a step, learning operating systems is another and hardware another, networking - internet - graphics - etc after enough steps your a competent hacker....G4143 - scaryblackguy - Jun 3, 2009 (Jun 3, 2009 09:54 PM)g4143 Wrote: One thing a lot of people do is assume that since you can program you understand how computers works. Programming is just a step, learning operating systems is another and hardware another, networking - internet - graphics - etc after enough steps your a competent hacker....G4143 very true, very true. Programming is indeed a very large step, but overall the bigger picture is the most important. You're a pretty wise guy g4143 |