标签: binary

为什么是myArrayList.size(); 产生不正确/巨大的数字?

基本上,我正在尝试编写一个程序,将数字从基数2转换为基数10.我尝试做的是将本网站上列出的"加倍方法"下的过程转换为for循环,但由于某种原因,数字我变得很大.

基本公式是(2*previousTotal)+(保存用户输入的二进制数的ArrayList的currentDigit)= previousTotal.

所以对于二进制的1011001,数学将是:

  • (0 x 2)+ 1 = 1
  • (1 x 2)+ 0 = 2
  • (2 x 2)+ 1 = 5
  • (5 x 2)+ 1 = 11
  • (11x 2)+ 0 = 22
  • (22 x 2)+ 0 = 44
  • (44 x 2)+ 1 = 89

然而,控制台打印出6185作为结果.我想它可能与我使用字符的ArrayList有关,但是charWhole.size()返回7,这是用户的二进制数中有多少位数.我一做charsWhole.get(w); 然而,我开始得到像49这样的大数字.我真的很感激一些帮助!

我写了这个循环,并根据我在整个代码中放置的一些打印语句和我的变量addThis似乎是问题所在.控制台打印出最终总数为6185,而基数10中的1011001实际为89.

public static void backto2(){
    System.out.println("What base are you coming from?");
    Scanner backToB10 = new Scanner(System.in);
    int bringMeBack = backToB10.nextInt();

    //whole
    System.out.println("Please enter the whole number part of your number.");
    Scanner eachDigit = …
Run Code Online (Sandbox Code Playgroud)

java binary for-loop arraylist

0
推荐指数
1
解决办法
94
查看次数

使用递归的二进制搜索

我无法弄清楚为什么这不会返回键,它似乎是跳过步骤,我觉得逻辑是直的,如果midptr小于键然后搜索右边的其他搜索左侧.但它没有返回键,它返回-1.救命?这是代码和功能

#include<iostream>
using namespace std;


int binsrch(int *raw, unsigned int size, int key);




int main()
{
  int raw[] = {1,3,5,7,11,23, 48};
  cout << binsrch(raw, 7, 11) << endl;


  system("pause");
  return 0;
}



int binsrch(int *raw, unsigned int size, int key)
{
    int *begptr, *endptr ,*midptr;
//see if we already have the key

if(*raw == key)
  return key;

begptr = raw;
endptr = raw + (size - 1);
midptr = raw + (size / 2);

cout << "#" <<*midptr << " …
Run Code Online (Sandbox Code Playgroud)

c++ binary search

0
推荐指数
1
解决办法
2192
查看次数

C - 有没有办法用比特检查检查一个数字是否等于1?

这只是一个感兴趣的问题 - 我不确定它实际上是如何适用的.无论如何 - 是否可以使用位检查来查看数字是否等于1?

c binary bit-manipulation bit

0
推荐指数
1
解决办法
839
查看次数

将0写入二进制文件C++

我想写一系列0到二进制文件.作为一个char,这应该是一个空格,但是,当我写入我的文件时,我收到了许多其他奇怪的字符.我不是写零,而是看似别的东西.

我这样做了吗?

码:

int zero = 0;
myfile.write(reinterpret_cast<char *>(&zero),1790*sizeof(char));    
Run Code Online (Sandbox Code Playgroud)

c++ binary file

0
推荐指数
2
解决办法
4178
查看次数

为什么这个二进制转换不起作用?

#include<stdio.h>
#include<conio.h>

unsigned * bin(unsigned n) {
    unsigned a[16];
    int i = 0, j = 0;
    for (i = 0; i < 16; i++) {
        a[i] = n & 0x1;
        n = n >> 1;
    }
    return a;
}

void main() {
    unsigned n = 5;
    int i = 0;
    unsigned * a = bin(n);
    for (i = 15; i >= 0; i--) {
        printf("%d\n", (*(a + i)));
    }
    getch();
}
Run Code Online (Sandbox Code Playgroud)

请帮助这个二进制转换不起作用.我正在尝试x^n使用二进制转换进行计算.可以任意帮助??

c c++ binary

0
推荐指数
1
解决办法
126
查看次数

十进制到二进制转换输出

我的代码有什么问题,任何人帮助我plz.这是一个十进制到二进制的转换.根据我的代码,输出将是2为10,3为11但它输出总是添加最后一个值,如3显示1110,添加以前的输出.我现在应该怎么做 ?帮我PLZ?

#include<iostream>
#include<stdio.h>
using namespace std;

int main(){

    long int decimalNumber,quotient;

    int binaryNumber[100],i=0,j;

    printf("Enter any decimal number: ");

    //scanf_s("%ld",&decimalNumber);
    while(scanf_s("%ld",&decimalNumber)==1)
    {

            quotient = decimalNumber;

            while(quotient!=0){
             binaryNumber[i++]= quotient % 2;
             quotient = quotient / 2;
           }

           printf("Equivalent binary value of decimal number %d: ",decimalNumber);

           for(j = i -1 ;j>= 0;j--)
               printf("%d",binaryNumber[j]);
           printf("\n");
           printf("Enter any decimal number: ");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c c++ binary decimal

0
推荐指数
1
解决办法
463
查看次数

Python函数返回None(检查所有简单的解决方案,它们不起作用)

现在,我已经编写了Python的二进制搜索(版本2.7).有时,它工作正常,但有时它会返回None,尽管搜索的值在数组中.我已经尝试了解决这个问题的每个简单方法:我已经检查了函数返回的变量是否被定义,是否执行了返回语句所在的工作流的分支.和:变量定义,则分支执行.

这是代码:

def binarySearch( array, desiderata, iMin, iMax ):
# Returns the index of the first instance of what we search
print 'min'
print iMin
print 'max'
print iMax

# If our search array is empty
if ( iMin > iMax ):
    return None

midP = (iMin + iMax)/2
curre = tapeNr( array[midP][TAPE_NUMBER] )
final = tapeNr( desiderata )
print 'curre'
print curre
print 'final'
print final
print 'midP'
print midP

if ( curre < final …
Run Code Online (Sandbox Code Playgroud)

python binary return function

0
推荐指数
1
解决办法
266
查看次数

从矩阵中删除列

我正在使用Matlab,我有以下问题.我有一个矩阵让我们说A

A =

 1     0     0     1     0     0
 1     0     1     0     0     0
Run Code Online (Sandbox Code Playgroud)

我希望能够删除只有零的列,但只能在最后一列之后至少有一个1列,即第5列和第6列但不是第2列.我不能这样做,A(:,5)=[]因为在我的问题中我不知道哪些列有零,他们到最后.

binary matlab matrix

0
推荐指数
1
解决办法
123
查看次数

按位运算符,为什么在应用了NOT运算符的二进制数中加1会使其为负值而不是不同的数字?

我刚刚开始学习Bitwise运算符(尽管我很难找到任何绝对的初学者资源).我理解二进制如何工作以及如何阅读但我不理解以下内容.

如果您使用二进制数37: 00100101并将〜(NOT)运算符应用于它,这些位将被翻转到 11011010 教程中我正在阅读它说你必须添加一个作为一种标志来表示它是否定的.所以它变成了11011011.我不明白为什么这个新号码不仅仅是219.

在相关的说明中,任何人都有任何深入的解释,解释我可以用来真正了解所有这些的所有资源,并最终理解这一切吗?

binary bit-manipulation

0
推荐指数
1
解决办法
1634
查看次数

将3个字节/引脚值组合成一个字节

我有4个引脚值定义P1_1,P1_2,P1_3,P1_4,带有位值.(1/0)

我想将它们组合成一个字节值,例如:

0000 0101(3 LSB为引脚)

我怎样才能做到这一点?

c c++ binary

0
推荐指数
1
解决办法
81
查看次数

标签 统计

binary ×10

c++ ×5

c ×4

bit-manipulation ×2

arraylist ×1

bit ×1

decimal ×1

file ×1

for-loop ×1

function ×1

java ×1

matlab ×1

matrix ×1

python ×1

return ×1

search ×1