หมายเลขบิตย้อนกลับ (ไบนารี) คืออะไร?


33

ดังนั้นคุณจะได้รับจำนวนฐาน 10 (ฐานสิบ) บวก งานของคุณคือกลับเลขฐานสองและส่งกลับเลขฐาน 10 นั้น

ตัวอย่าง:

1 => 1 (1 => 1)
2 => 1 (10 => 01)
3 => 3 (11 => 11)
4 => 1 (100 => 001)
5 => 5 (101 => 101)
6 => 3 (110 => 011)
7 => 7 (111 => 111)
8 => 1 (1000 => 0001)
9 => 9 (1001 => 1001)
10 => 5 (1010 => 0101)

นี่คือความท้าทายในการดังนั้นวิธีการแก้ปัญหาที่ใช้ไบต์น้อยที่สุดชนะ

นี่คือA030101ใน OEIS


2
"reverse the bits" หมายถึงการกลับหลักเลขฐานสองหรือไม่ บางครั้งก็ยังหมายถึงการกลับทุกบิต
— ETHproductions

ใช่. ขออภัยที่ไม่ชัดเจน
— juniorRubyist

สิ่งนี้และสิ่งนี้คล้ายกัน
— Geobits

OEIS A030101
— orlp

1
"ฐาน 10" เหตุผลใดเหตุผลพิเศษทำไม
— เครื่องคิดเลข

คำตอบ:


20

Pythonขนาด 29 ไบต์

lambda n:int(bin(n)[:1:-1],2)

ลองออนไลน์!

นี่คือฟังก์ชันที่ไม่ระบุชื่อและไม่มีชื่อซึ่งจะส่งคืนผลลัพธ์


ขั้นแรกให้bin(n)แปลงอาร์กิวเมนต์เป็นสตริงไบนารี [::-1]เราปกติจะย้อนกลับนี้กับโน้ตชิ้น สิ่งนี้จะอ่านสตริงด้วยขั้นตอนที่-1คือย้อนกลับ อย่างไรก็ตามสตริงไบนารีในหลามจะมีคำนำหน้ากับ0bและดังนั้นเราให้อาร์กิวเมนต์ที่สองหั่นในฐานะที่เป็น1บอกหลามอ่านย้อนหลังยุติการที่ดัชนี1จึงไม่ได้อ่านดัชนี1และ0

ตอนนี้เรามีสตริงไบนารีข้างหลังเราจะผ่านมันไปint(...)ด้วยอาร์กิวเมนต์ที่สองเป็น2 นี่จะอ่านสตริงเป็นจำนวนเต็มฐาน 2 ซึ่งก็จะส่งกลับนัยยะของการแสดงออกแลมบ์ดา


2
เอาชนะคุณ 9 วินาที
— mbomb007


6
@ mbomb007 คำตอบของฉันไม่ถูกต้องเพราะคุณคลิกปุ่มโพสต์ 9 วินาทีก่อนถึงมือ? เพียงเพราะเราไปถึงสนามกอล์ฟเดียวกันในเวลาเดียวกันไม่ได้หมายความว่าเราต้องลบคำตอบใด ๆ หากมีสิ่งใดให้โทษคำถาม 0 ความพยายาม
— FlipTack

3
ไม่ถูกต้อง แต่ไม่มีจุดหมายแน่นอน ถ้าฉันช้าลงฉันก็แค่ลบของฉันและโพสต์ความคิดเห็นเกี่ยวกับสิ่งที่เร็วกว่าที่ฉันคิดไว้ด้วย
— mbomb007

1
@steenbergh ใครสนใจ? รหัสเดียวกันคะแนนเดียวกัน
— mbomb007


13

JavaScript (ES6), 30 28 ไบต์

บันทึก 2 ไบต์ขอบคุณ @Arnauld

f=(n,q)=>n?f(n>>1,q*2|n%2):q

สิ่งนี้จะคำนวณย้อนกลับทีละบิต: เราเริ่มต้นด้วยq = 0 ; ในขณะที่nเป็นบวกคูณเราQ 2, ตัดบิตสุดท้ายออกจากnด้วยn>>1และเพิ่มเข้าไปในคิว|n%2ด้วย เมื่อnต้นน้ำ 0 จำนวนได้กลับประสบความสำเร็จและเรากลับQ

ต้องขอบคุณชื่อที่มีมานานของ JS การแก้ปัญหานี้วิธีที่ง่ายใช้เวลา 44 ไบต์:

n=>+[...n.toString(2),'0b'].reverse().join``

การใช้การเรียกซ้ำและสตริงคุณจะได้โซลูชัน 32 ไบต์ที่ทำสิ่งเดียวกัน:

f=(n,q='0b')=>n?f(n>>1,q+n%2):+q

f=(n,q)=>n?f(n>>1,q*2|n%2):qเกือบจะได้ผล n=0แต่เศร้าไม่ได้สำหรับ
— Arnauld

@Arnauld OP ยังไม่ได้ตอบว่าข้อมูลจะเป็นค่าบวกหรือไม่ แต่ถ้าเป็นเช่นนั้น 0 จะไม่ได้รับการจัดการ
— FlipTack

นี่เป็นการติดตามผลล่าช้า แต่ขณะนี้ข้อมูลเข้าเป็นที่ทราบกันดีเสมอ
— Arnauld

@Arnauld ขอบคุณ!
— ETHproductions

10

Java 8, 53 47 46 45 ไบต์

  • -4 ไบต์ขอบคุณ Titus
  • -1 ไบต์ต้องขอบคุณ Kevin Cruijssen

นี่คือการแสดงออกแลมบ์ดาซึ่งมีหลักการเดียวกันกับคำตอบของ ETH (แม้ว่าการเรียกซ้ำจะมีความละเอียดมากเกินไปใน Java ดังนั้นเราจึงวนซ้ำแทน):

x->{int t=0;for(;x>0;x/=2)t+=t+x%2;return t;}

ลองออนไลน์!

นี้สามารถที่ได้รับมอบหมายด้วยแล้วเรียกว่ามีIntFunction<Integer> f = ... f.apply(num)ขยายตัวไม่อัปโหลดและแสดงความคิดเห็นดูเหมือนว่า:

x -> { 
    int t = 0;           // Initialize result holder   
    while (x > 0) {      // While there are bits left in input:
        t <<= 1;         //   Add a 0 bit at the end of result
        t += x%2;        //   Set it to the last bit of x
        x >>= 1;         //   Hack off the last bit of x
    }              
    return t;            // Return the final result
};

1
บันทึกด้วย 3 ไบต์t*2แทน(t<<1)อีกหนึ่งการย้ายการคำนวณนั้นจากส่วนหัวของลูปไปยังส่วนของลูป คุณสามารถใช้xแทนx>0เงื่อนไขได้หรือไม่?
— ติตัส

2
@Tios ไม่ได้โดยไม่ต้องมีการหล่อเพื่อบูลีน แต่ขอบคุณสำหรับเคล็ดลับอื่น ๆ ! เพิ่งรู้ว่าx>>=1สามารถถูกแทนที่ด้วยx/=2มันจะเป็นจำนวนเต็มหารโดยอัตโนมัติ
— FlipTack

45 bytes (เปลี่ยนt=t*2+เป็นt+=t+)
— Kevin Cruijssen

@KevinCruijssen เป็นคนดีคนหนึ่ง!
— FlipTack


8

เยลลี่ 3 ไบต์

BUḄ

ลองออนไลน์!

B   # convert to binary
 U  # reverse
  Ḅ # convert to decimal

7
สั้นมาก BUB
— Cyoce

อืม ... นั่นเป็น 3 ไบต์จริงเหรอ?
— aioobe

1
@aioobe อ๋อ เยลลี่ใช้หน้าโค้ดของตัวเองโดยที่อักขระแต่ละตัวมีขนาด 1 ไบต์
— Riley

เจ๋งขอบคุณ! <pad>
— aioobe


7

เขาวงกต 23 ไบต์

?_";:_2
  :   %
 @/2_"!

ดีนี่มันน่าอึดอัดใจ ... นี่จะคืนค่าหมายเลข BINARY ย้อนกลับ ... ขอบคุณ @Martin Ender สำหรับการชี้ให้เห็นข้อผิดพลาดของฉันและข้อผิดพลาด ID 10T ของฉัน ดังนั้นวิธีนี้ใช้ไม่ได้ฉันจะต้องหาวิธีแก้ปัญหาอื่น


1
ยินดีต้อนรับสู่ PPCG และโพสต์แรกที่ดี! การทำสิ่งที่ท้าทายในภาษาอย่าง Labyrinth อาจเป็นเรื่องยากมาก แถวนี้เรามักจะนำหน้าบรรทัดแรกของคำตอบด้วยหนึ่งหรือสองแฮชเพื่อให้มันแสดงเป็นส่วนหัว:# Labyrinth, 89 bytes
— ETHproductions

1
คุณไม่ได้เว้นช่องผู้นำจากแถวที่สองโดยบังเอิญหรือไม่? เนื่องจากมันยืนอยู่โปรแกรมจะเด้งกลับไปกลับมาในบรรทัดแรกเพราะ_อยู่บนทางแยก
— Martin Ender

น่าเสียดายที่ฉันเพิ่งสังเกตเห็นว่าสิ่งนี้ไม่ถูกต้องไม่ว่าจะเป็นเพราะความท้าทายขอให้ตัวแทน 10 ฐานของตัวเลขที่กลับรายการไม่ใช่การเป็นตัวแทนไบนารี
— Martin Ender

6

C, 48 44 43 42 ไบต์

-1 ไบต์ขอบคุณ gurka และ -1 ไบต์ขอบคุณ anatolyg:

r;f(n){for(r=n&1;n/=2;r+=r+n%2);return r;}

โซลูชัน 44 ไบต์ก่อนหน้า:

r;f(n){r=n&1;while(n/=2)r=2*r+n%2;return r;}

โซลูชัน 48 ไบต์ก่อนหน้า:

r;f(n){r=0;while(n)r=2*(r+n%2),n/=2;return r/2;}

Ungolfed และการใช้งาน:

r;
f(n){
 for(
  r=n&1;
  n/=2;
  r+=r+n%2
 );
 return r;}
}


main() {
#define P(x) printf("%d %d\n",x,f(x))
P(1);
P(2);
P(3);
P(4);
P(5);
P(6);
P(7);
P(8);
P(9);
P(10);
}

ไม่rได้เริ่มต้นเป็นศูนย์ที่นี่r;f(n){r=0;แล้วเช่นr=0;ไม่จำเป็น? นอกจากนี้ตัวพิมพ์เล็ก: " การแก้ปัญหา48ไบต์ก่อนหน้า"
— simon

1
@gurka ฟังก์ชันนี้สามารถนำมาใช้ซ้ำได้
— Karl Napf

1
ฉันคิดว่าforลูปนั้นสั้นอย่างน้อยเท่ากับwhileลูปและมักจะสั้นกว่าเสมอ
— Anatolyg

@anatolyg สิ่งที่ชอบr;f(n){for(r=n&1;n/=2;r=2*r+n%2);return r;}? สั้นลง 1 ไบต์ แต่ฉันไม่แน่ใจว่ามันถูกต้อง C (C99)
— simon

ใช่; นอกจากนี้ยังมีการเปิด=เข้าไป+=จะทำให้มันสั้นและยุ่งเหยิงมากขึ้น
— anatolyg

5

Ruby, 29 28 ไบต์

->n{("%b"%n).reverse.to_i 2}

"% b"% n จัดรูปแบบอินพุต n เป็นสตริงไบนารีย้อนกลับจากนั้นแปลงกลับเป็นตัวเลข

การใช้งาน / กรณีทดสอบ:

m=->n{("%b"%n).reverse.to_i 2}
m[1] #=> 1
m[2] #=> 1
m[3] #=> 3
m[4] #=> 1
m[5] #=> 5
m[6] #=> 3
m[7] #=> 7
m[8] #=> 1
m[9] #=> 9
m[10] #=> 5

@Titus ฉันคิดว่าคุณเข้าใจผิดคำตอบ 2เป็นฐานที่เขาแปลงไปและnเป็นอินพุต ->args{return value}เป็นไวยากรณ์แลมบ์ดาทับทิม
— Cyoce

คุณสามารถเอาวงเล็บออกได้.to_i(2)หรือไม่?
— Cyoce

@Cyoce ขอบคุณมากพอ
— Alexis Andersen


4

Java (OpenJDK), 63 bytes

a->a.valueOf(new StringBuffer(a.toString(a,2)).reverse()+"",2);

Try it online!

Thanks to poke for -12 bytes and to Cyoce for -8 bytes!


Even though REPL submissions are allowed, they still follow the rule that you can't assume input is in predefined variables (like a in this context)
— FlipTack

@FlipTack Oops. It was originally a function before I remembered the repl existed
— Pavel

1
Also, in the future, use print instead of println for golfing :)
— FlipTack

1
StringBuffer saves a byte over StringBuilder
— Poke

1
Could you do +"" instead of .toString()?
— Cyoce

3

Perl 6, 19 bytes

{:2(.base(2).flip)}

Where is the input?
— Titus

This is a function that takes a single parameter $_. It isn't mentioned by name, but the base method is called on it.
— Sean

2
@Titus in Perl 6 a Block is a type of Code, which is to say it's a callable object. The above is an expression that you can take and assign to a variable like a function or lambda in another language, or call directly — {:2(.base(2).flip)}(10) at the REPL will print 5. So it meets the standard code-golf criteria for a function.
— hobbs



3

PHP, 33 bytes

<?=bindec(strrev(decbin($argn)));

convert to base2, reverse string, convert to decimal. Save to file and run as pipe with -F.

no builtins:

iterative, 41 bytes

for(;$n=&$argn;$n>>=1)$r+=$r+$n%2;echo$r;

While input has set bits, pop a bit from input and push it to output. Run as pipe with -nR.

recursive, 52 bytes

function r($n,$r=0){return$n?r($n>>1,$r*2+$n%2):$r;}

@JörgHülsermann The 44 bytes have $r+=$r. But I actually don´t remember why I put that in front.
— Titus




2

Scala, 40 bytes

i=>BigInt(BigInt(i)toString 2 reverse,2)

Usage:

val f:(Int=>Any)=i=>BigInt(BigInt(i)toString 2 reverse,2)
f(10) //returns 5

Explanation:

i =>          // create an anonymous function with a parameter i
  BigInt(       //return a BigInt contructed from
    BigInt(i)     //i converted to a BigInt
    toString 2    //converted to a binary string
    reverse       //revered
    ,           
    2             //interpreted as a binary string
  )




1

Batch, 62 bytes

@set/an=%1/2,r=%2+%1%%2
@if %n% gtr 0 %0 %n% %r%*2
@echo %r%

Explanation: On the first pass, %1 contains the input parameter while %2 is empty. We therefore evaluate n as half of %1 and r as +%1 modulo 2 (the % operator has to be doubled to quote it). If n is not zero, we then call ourselves tail recursively passing in n and an expression that gets evaluated on the next pass effectively doubling r each time.



1

R, 55 bytes

sum(2^((length(y<-rev(miscFuncs::bin(scan()))):1)-1)*y)

Reads input from stdin and consequently uses the bin function from the miscFuncs package to convert from decimal to a binary vector.


1

Pushy, 19 bytes

No builtin base conversion!

$&2%v2/;FL:vK2*;OS#

Try it online!

Pushy has two stacks, and this answer makes use of this extensively.

There are two parts two this program. First, $&2%v2/;F, converts the number to its reverse binary representation:

            \ Implicit: Input is an integer on main stack.
$      ;    \ While i != 0:
 &2%v       \   Put i % 2 on auxiliary stack
     2/     \   i = i // 2 (integer division)
        F   \ Swap stacks (so result is on main stack)

Given the example 10, the stacks would appear as following on each iteration:

1: [10]
2: []

1: [5]
2: [0]

1: [2]
2: [0, 1]

1: [1]
2: [0, 1, 0]

1: [0]
2: [0, 1, 0, 1]

We can see that after the final iteration, 0, 1, 0, 1 has been created on the second stack - the reverse binary digits of 10, 0b1010.

The second part of the code, L:vK2*;OS#, is taken from my previous answer which converts binary to decimal. Using the method decsribed and explained in that answer, it converts the binary digits on the stack into a base 10 integer, and prints the result.



0

C#, 167 bytes

 for(int i = 1; i <= 10; i++)
 {
 var bytes= Convert.ToString(i, 2);
 var value= Convert.ToInt32(byteValue.Reverse()); 
 console.WriteLine(value);
}

Explanation:

Here I will iterate n values and each time iterated integer value is convert to byte value then reverse that byte value and that byte value is converted to integer value.


1
Welcome to the site! I don't know much about C# but you most certainly have a good deal of extra whitespace I would recommend removing. It also is not clear how I/O is dealt with in this submission. It is standard to either write a function or to use STDIN (I think that is console.Read() but you would probably know better than I would) and STDOUT. Anyway, welcome to the site if you want more experienced advice in golfing C# I would recommend codegolf.stackexchange.com/questions/173/…
— Wheat Wizard

I've downvoted this answer, because it doesn't work at all. .Reverse() returnes IEnumerable<char>. As Convert.ToInt32 doesn't have an overload for IEnumerable it throws an exception. Also the answer doesn't follow the rules for code golf: 1)As nothing is specified the submission has to be a full program or function not just a snippet. 2)using statements must be included in the byte count
— raznagul

0

c/c++ 136 bytes

uint8_t f(uint8_t n){int s=8*sizeof(n)-ceil(log2(n));n=(n&240)>>4|(n&15)<<4;n=(n&204)>>2|(n&51)<<2;n=(n&172)>>1|(n&85)<<1;return(n>>s);}

It's not going to win, but I wanted to take a different approach in c/c++ 120 bytes in the function

#include <math.h>
#include <stdio.h>
#include <stdint.h>

uint8_t f(uint8_t n){
    int s=8*sizeof(n)-ceil(log2(n));
    n=(n&240)>>4|(n&15)<<4;
    n=(n&204)>>2|(n&51)<<2;
    n=(n&172)>>1|(n&85)<<1;
    return (n>>s);
}

int main(){
    printf("%u\n",f(6));
    return 0;
}

To elaborate on what I am doing, I used the log function to determine the number of bits utilized by the input. Than a series of three bit shifts left/right, inside/outside, even/odd which flips the entire integer. Finally a bit shift to shift the number back to the right. Using decimals for bit shifts instead of hex is a pain but it saved a few bytes.


You do need to include the function declaration, so this is actually 163 bytes. Although, if you remove the extraneous whitespace, you could shorten it to 136.
— DJMcMayhem
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.