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;
    }
相关推荐
SoftLipaRZC5 分钟前
C语言数据在内存中的存储:整型与浮点型的秘密
c语言·开发语言
jghhh018 分钟前
基于 Weiler-Atherton 算法的多边形裁剪程序实现
算法
不爱吃糖の糖糖9 分钟前
RAG 04:向量数据库与索引算法
数据库·算法
MegaDataFlowers10 分钟前
226.翻转二叉树
算法
社交怪人11 分钟前
【2的幂】信息学奥赛一本通C语言解法(题号1037)
c语言
alphaTao26 分钟前
LeetCode 每日一题 2026/5/25-2026/5/31
算法·leetcode
菜菜的顾清寒29 分钟前
力扣HOT100(41)动态规划-杨辉三角
算法·leetcode·动态规划
Cthy_hy35 分钟前
Python算法竞赛:集合去重+字典映射 核心用法一站式整理
数据结构·python·算法
wyjcxyyy37 分钟前
java反序列化-cc1链
java·c语言·开发语言
Deepoch41 分钟前
Deepoc数学大模型:驱动发动机行业数智化转型的底层解
人工智能·算法·deepoc·数学大模型