From 73177d33486491e92670c3f91cb43c0653c8f5f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B5=B7=E6=B4=8B?= <517573303@qq.com> Date: Sat, 25 Dec 2021 13:33:28 +0000 Subject: [PATCH] =?UTF-8?q?add=20day1/=E4=BD=9C=E4=B8=9A/GO7031-=E6=9D=8E?= =?UTF-8?q?=E6=B5=B7=E8=B6=85/homework1.txt.=20=E7=AC=AC=E4=B8=80=E5=A4=A9?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homework1.txt" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "day1/\344\275\234\344\270\232/GO7031-\346\235\216\346\265\267\350\266\205/homework1.txt" diff --git "a/day1/\344\275\234\344\270\232/GO7031-\346\235\216\346\265\267\350\266\205/homework1.txt" "b/day1/\344\275\234\344\270\232/GO7031-\346\235\216\346\265\267\350\266\205/homework1.txt" new file mode 100644 index 0000000..44ddfd1 --- /dev/null +++ "b/day1/\344\275\234\344\270\232/GO7031-\346\235\216\346\265\267\350\266\205/homework1.txt" @@ -0,0 +1,27 @@ +package main +import ( + "fmt" + "math" + "strings" +) +func BinaryFormat(n int32) string { + a := uint32(n) + sb := strings.Builder{} + c := uint32(math.Pow(2, 31)) + for i := 0; i < 32; i++ { + if a&c != 0 { + sb.WriteString("1") + } else { + sb.WriteString("0") + } + c >>= 1 + } + return sb.String() +} +func main() { + fmt.Println(BinaryFormat(0)) + fmt.Println(BinaryFormat(1)) + fmt.Println(BinaryFormat(-1)) + fmt.Println(BinaryFormat(260)) + fmt.Println(BinaryFormat(-260)) +} \ No newline at end of file -- Gitee