首页 \ 问答 \ 理解反转偶数二进制表示的函数(Understanding function which inverts binary representation of even number)

理解反转偶数二进制表示的函数(Understanding function which inverts binary representation of even number)

我最近偶然发现这个函数反转了偶数的二进制表示,但是我无法理解代码背后的逻辑?

int binaryReverse(int toReverse) {

     int reversed = 0;
     while(toReverse > 0) {
             reversed *= 2;
             reversed += toReverse % 2;
             toReverse /= 2;
     }

     return reversed;
}

I recently stumbled upon this function which inverts binary representation of even numbers,but I'm not able to understand the logic behind the code?

int binaryReverse(int toReverse) {

     int reversed = 0;
     while(toReverse > 0) {
             reversed *= 2;
             reversed += toReverse % 2;
             toReverse /= 2;
     }

     return reversed;
}

原文:https://stackoverflow.com/questions/22335155
更新时间:2024-01-31 09:01

最满意答案

flatMap()运算符采用一个附加参数,即它维护的同时订阅数。 这是一个测试样本:

@Test
public void testStuff() throws Exception {
    Single<String> source;
    PublishSubject<String> src = PublishSubject.create();
    src
            .flatMap( s -> doStuff( s ).toObservable(), 5 )
            .subscribe( v -> { // process v
            } );
}

Single<String> doStuff( String subject ) {
    return Single.just( subject );
}

如果doStuff()返回一个Single ,则必须将其转换为Observable ,因为这是flatMap()工作原理。


The flatMap() operator takes an additional parameter which is the number of simultaneous subscriptions it maintains. Here is a test sample:

@Test
public void testStuff() throws Exception {
    Single<String> source;
    PublishSubject<String> src = PublishSubject.create();
    src
            .flatMap( s -> doStuff( s ).toObservable(), 5 )
            .subscribe( v -> { // process v
            } );
}

Single<String> doStuff( String subject ) {
    return Single.just( subject );
}

If doStuff() returns a Single, you have to convert it to an Observable, since that is what flatMap() works with.

相关问答

更多
  • 听起来你正在用完临时端口 。 要检查,请使用netstat命令并查找处于TIME_WAIT状态的数千个端口。 在Mac OS X上,默认的临时端口范围为49152至65535,总共为16384个端口。 您可以使用sysctl命令来检查这一点: $ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last net.inet.ip.portrange.first: 49152 net.inet.ip.portrange.last: 65535 ...
  • 听起来像会话阻塞问题 默认情况下,PHP会将其会话数据写入文件。 当您使用session_start()启动会话时,它将打开文件进行写入并锁定它,以防止并发编辑。 这意味着对于使用会话通过PHP脚本的每个请求,都必须等待第一个会话完成。 解决这个问题的方法是将PHP会话更改为不使用文件或关闭会话,如下所示:
  • 我不确定为什么在第一次运行时没有得到OOM错误的第二次运行时会出现OOM错误; 当你运行Perl脚本并且perl二进制文件退出时,它会将所有内存释放回操作系统。 执行之间没有任何内容。 每次REST服务返回的数据是否完全相同? 也许第二次运行时会有更多的数据,它会让你超越边缘。 我注意到的一个问题是你正在启动10个线程并运行它们完成,然后产生10个以上的线程。 更好的解决方案可能是工作线程模型。 在程序开始时生成10个线程(或多个你想要的),将URL放入队列,并允许线程自己处理队列。 这是一个可能有帮助的简 ...
  • 你可以这样做 Observable .fromArray(/*your list of observables go here, make sure that within flatMap you get as type Observable, not Observable>*/) .flatMap(/*here you subscribe every item to a different thread, so they're parallel requests: s ...
  • 我能找到的唯一解决方案是存储无法通过测试函数的请求,然后使用boost库的wait或wait_all函数等待它们。 The only solution that I could find is storing the requests which could not pass the test function, then waiting them using wait or wait_all function of boost library.
  • 正如您发现的, async.parallel()只能并行处理本身异步的操作。 如果操作是同步的,那么由于node.js的单线程特性,操作将一个接一个地运行,而不是并行运行。 但是,如果操作本身是异步的,那么async.parallel() (或其他异步方法)将立即启动它们并协调结果。 这是使用async.map()的一般想法。 我使用async.map()是因为它的想法是它需要一个数组作为输入,并以与原始数据相同的顺序生成一个结果数组,但并行运行所有请求,这似乎与您想要的一致: var async = re ...
  • flatMap()运算符采用一个附加参数,即它维护的同时订阅数。 这是一个测试样本: @Test public void testStuff() throws Exception { Single source; PublishSubject src = PublishSubject.create(); src .flatMap( s -> doStuff( s ).toObservable(), 5 ) ...
  • 如果您真的需要通过GET请求来实现,我建议您使用带有小线程池(2或3)的ThreadPoolExecutor,以避免重载维基百科服务器。 这将避免大量编码...... 还要考虑使用Apache HttpClient库(具有持久连接!)。 但是使用数据库下载选项要好得多。 根据您的操作,您可以选择较小的下载之一。 本页讨论了各种选项。 注意:维基百科更喜欢人们下载数据库转储(等等),而不是在他们的Web服务器上敲击。 If you really, really need to do it via GET re ...
  • 最简单的选择是使用async方法: async Task DownloadAndUpdate(File file) { await file.DownloadAsync(); UpdateUI(file) } 而等待这些任务: await Task.WhenAll(files.Select(file => DownloadAndUpdate(file))); 如果方法太多,你可以用一个lambda表达式做到这一点: await Task.WhenAll(files.Select(asyn ...
  • 正如您所提到的,如果searchTrack()返回Promise , Promise.all()可以帮助您。 所以这样的事情: searchTrack(searchParams: SongSearchParams, type='track') { // ... return this.http.get(this.user_url, {headers : headers}) .toPromise() .then(response => { // Code from you ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)