c语言实现循环位移的函数

  • 循环左移

    c 复制代码
    #include <stdio.h>
    int lefttrot(int x, int n){
        if (n < 0) {
            printf("移动数目不可为负");
            return -1;
        }
        
        n %= 32;
        int book = x & (-1 >> (32 - n));
        return (x >> n) | (book << (32 - n));
    }
    
    int main(){
        printf("%d", lefttrot(-1, 100));
        return 0;
    }
  • 循环右移

    c 复制代码
    #include <stdio.h>
    int righttrot(int x, int n){
        if (n < 0) {
            printf("移动数目不可为负");
            return -1;
        }
        
        n %= 32;
        int book = x >> (32 - n);
        return (x << n) | book;
    }
    
    int main(){
        printf("%d", righttrot(-1, 100));
        return 0;
    }
相关推荐
生态学者6 小时前
Global Change Biology | 森林越“步调一致”,反而越怕干旱?树木生长与叶片活动的同步之谜
大数据·人工智能·算法·r语言·微信公众平台
lsswear6 小时前
Agent 框架新手入门与实战部署指南
大数据·人工智能·算法
学逆向的7 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
Data analyse4568 小时前
rnweb 回放数据量太大怎么办?分片存储与断点续传的实现思路
前端·网络·算法·数据分析
Navigator_Z9 小时前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode
Nil20810 小时前
leetcode 64最小路径和
算法·leetcode·职场和发展
光影少年10 小时前
AI Agent、自动化与未来工作
人工智能·算法·架构
牧羊人.33310 小时前
计算机视觉基础 第16章|EigenFace,FisherFace,LBPH传统人脸识别算法
人工智能·算法·计算机视觉
jeo陽10 小时前
04-循环实战:素数、最大公约数、级数求和与枚举法
c语言·开发语言·数据结构
Nil20810 小时前
leetcode 72编辑距离
算法·leetcode·职场和发展